Thread: Linked list reverse recursively

  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59

    Linked list reverse recursively

    Hi, I am trying to reverse a linked list recursively. But the problem is that I am not able to understand the code.
    Here's the code:

    Code:
    void recursiveReverse(struct node** head_ref)
    {
        struct node* first;
        struct node* rest;
     
        /* empty list */
        if (*head_ref == NULL)
           return;  
     
        /* suppose first = {1, 2, 3}, rest = {2, 3} */
        first = *head_ref;
        rest  = first->link;
     
        /* List has only one node */
        if (rest == NULL)
           return;  
     
        /* put the first element on the end of the list */
        recursiveReverse(&rest);
        first->link->link  = first; 
     
        /* tricky step -- see the diagram */
        first->link  = NULL;         
     
        /* fix the head pointer */
        *head_ref = rest;
    }
    Let the input linked list be 1->2->3->4

    Here, I am not getting how the head_ref variable points to 4 at the end of the program. What I get is that head_ref should point to 2 instead of 4 as it is equal to rest.
    Maybe I am facing this problem because my recursion concepts are not clear. Any help will be appreciable.

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Alternatively... your problem could be that you are using other people's code instead of writing it yourself.

    The problem with scoop and poop coding is that you don't have to figure it out for yourself and thus never grasp the underlying concepts... You don't LEARN from it.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59
    I know I should write it myself but the problem is that when I don't understand how the above recursion works, I don't think I would be able to write it myself. I know what the above code is trying to do and the concepts but just a little stuck over the pointers being updated when the function returns. I have spent a lot of time trying it myself and then when I couldn't, I tried the above code, which is perfectly fine but maybe my concepts are not clear. Thanks for the reply, anyways.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by gaurav_13191 View Post
    I know I should write it myself but the problem is that when I don't understand how the above recursion works, I don't think I would be able to write it myself.
    That is not necessarily true "in general". Sometimes examples clear things up, sometimes they just make things murkier. Also, you can put yourself in more of a box if you begin to believe that a demonstration of a concept defines the concept itself.

    Fortunately, this one is fairly straightforward.

    I know what the above code is trying to do and the concepts but just a little stuck over the pointers being updated when the function returns. I have spent a lot of time trying it myself and then when I couldn't, I tried the above code, which is perfectly fine but maybe my concepts are not clear.
    All the action is here:

    Code:
        first = *head_ref;
        rest  = first->link;
    
    [...]
    
        /* put the first element on the end of the list */
        recursiveReverse(&rest);
        first->link->link  = first;
      
        /* tricky step -- see the diagram */
        first->link  = NULL;   
    
        /* fix the head pointer */
        *head_ref = rest;
    Recursion has a FILO ordering -- the first call is the last to return, the last call is the first to return.

    Let's say you pass this a four node list, 1->2->3->4.
    The second call then receives 2->3->4.
    The third call 3->4.
    The fourth call receives 4, and returns to 3.
    In three, the first node is 3, and first->link is 4, so 4->link is set to 3:
    Code:
      first->link->link  = first;
    The first->link is set to NULL and 4 is set to head, leaving 4->3 when the third call returns into the second.
    In the second call, 2 (the "first" node) has not been changed. That means 2->link still points to 3. So 3 now points to 2, and 2->NULL, leaving 4->3->2 when we return to the first call.
    In the first call -- the final one to complete -- 1->link is still 2. So first->link->link sets 2 to point to 1. Then first->link is set to NULL.

    Notice that the recursion is called using &rest, and that address is set to head in each successive call. The last call was the forth one, which only got passed one node (4). That means when the recursion completes and head is set to "rest" you'll have: 4->3->2->1.

    Make sense?
    Last edited by MK27; 12-07-2011 at 10:05 AM.

  5. #5
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59
    Ok.. thanks for the reply..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reverse a linked list, probably a simple fix.
    By csharp100 in forum C Programming
    Replies: 5
    Last Post: 11-10-2010, 11:54 PM
  2. Linked List(+reverse) trouble
    By killmequick in forum C Programming
    Replies: 8
    Last Post: 09-18-2008, 01:40 PM
  3. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  4. Dividing a linked list in half recursively
    By Lone in forum C++ Programming
    Replies: 3
    Last Post: 03-04-2005, 04:36 PM
  5. reverse a singly linked list
    By ivandn in forum C Programming
    Replies: 15
    Last Post: 12-18-2001, 05:33 PM