Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual Explanation Guidance needed for reversal of Linked List datastructure code?

I have following piece of code for reversing the linked list. I am getting confused in while loop and so would certainly appreciate if someone can provide visual explanation of how actually it's working.

 static void Reverse (struct node** headRef)
{
     struct node* result = NULL;
     struct node* current = *headref;
     struct node* next;

     while(current != NULL)
     {
        next = current->next;
        current->next = result;
        result = current;

        current = next;
     }     
     *headRef = result;

}
like image 201
Rachel Avatar asked Sep 26 '09 21:09

Rachel


People also ask

How do you display the elements of a linked list in reverse order?

To print a singly linked list in reverse order, we will use a recursive function. We will store the head node of linked list in function stack and then recursively call reverseLLPrint function for sub linked list starting from head->next.

How do I reverse content in LinkedList?

Reverse a LinkedList Using Recursive ApproachStep 1: Split the list given into two parts - the first node and the rest of the linked list. Step 2: Invoke the reverseList() method for the remaining portion of the linked list. Step 3: Join the rest to the first. Step 4: Fix the head pointer.

How do you reverse a list in C++?

list::reverse() is an inbuilt function in C++ STL which is declared in header file. reverse() is used to reverse the list container, means the last element of the list becomes the first element of the list.


2 Answers

OK, here's my attempt to make valya's answer even clearer (though I thought it was pretty good already):

Say we have this list:

// a->b->c->d->e->NULL

We start at the first node, a, which contains a pointer (next) to b:

// a->b ...

The line next = current->next; sets next to b (simple enough). The next line current->next = result; does this:

// NULL<-a  b ... (notice there is no longer a pointer from a to b)

Then we have result = current; which sets result to a (again, simple enough). And finally, we have the all important current = next;, which set current to b.

So on the next iteration of the while loop, with next set to b, result set to a, and current set to b, we start over:

next = current->next;

// NULL<-a<-b  c ...
current->next = result;

result = current;

Then we do it again:

next = current->next;

// NULL<-a<-b<-c  d ...
current->next = result;

result = current;

Once we've gotten to the last item in the linked list (e in this example), this happens:

next = current->next; // next becomes NULL

// NULL<-a<-b<-c<-d<-e
current->next = result;

result = current; // result is now e

current = next; // current is now NULL

Now, since current is NULL, the while loop terminates, and we are left with:

*headRef = result;

which, as you can see now, makes headRef point to e, treating e as the new first item in our linked list, with e->next pointing to d, d->next pointing to c, etc.

like image 171
Dan Tao Avatar answered Sep 19 '22 13:09

Dan Tao


I made a diagram in dot that I think will graphically explain what's going on:

thumbnail

Link to full-sized image

And here's the (sloppy) dot source in case anyone cares:

digraph g {
    label = "Start"
    subgraph cluster_1
    {
        a1 -> b1 -> c1;
        current1 -> a1;
        result1
        a1 [label="a"]
        b1 [label="b"]
        c1 [label="c"]
        current1 [label="current"]
        result1 [label="result"]
    }
    label = "Once through loop"
    subgraph cluster_2
    {
        current2 -> b2;
        result2 -> a2;
        b2 -> c2;
        a2 [label="a"]
        b2 [label="b"]
        c2 [label="c"]
        current2 [label="current"]
        result2 [label="result"]
    }
    label = "Twice through loop"
    subgraph cluster_3
    {
        current3 -> c3;
        result3 -> b3;
        b3 -> a3;
        a3 [label="a"]
        b3 [label="b"]
        c3 [label="c"]
        current3 [label="current"]
        result3 [label="result"]
    }
    label = "Final"
    subgraph cluster_4
    {
        result4 -> c4 -> b4 -> a4;
        a4 [label="a"]
        b4 [label="b"]
        c4 [label="c"]
        current4 [label="current"]
        result4 [label="result"]
    }
    label = ""

}
like image 41
Mark Rushakoff Avatar answered Sep 19 '22 13:09

Mark Rushakoff