Wednesday, 8 October 2014

One more way for reversing linked list


Reversing the linked list by changing pointers from forward direction.
struct node *rreverse(struct node *current, struct node **prev)
{
        struct node *next;

        if (!current)
                return *prev;

        next = current->next;
        current->next = *prev;
        *prev = current;

        return rreverse(next, prev);

...

        struct node *prev = NULL;
        head = rreverse(head, &prev);

No comments:

Post a Comment