Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The general recursive algorithm for this is:</p> <ol> <li><code>Divide</code> the list in <code>2</code> parts - first node and rest of the list.</li> <li>Recursively call reverse for the <code>rest</code> of the linked list.</li> <li>Link <code>rest</code> to <code>first</code>.</li> <li>Fix <code>head</code> pointer</li> </ol> <p>You are doing steps 1 and 2 correctly but I guess you've messed up in steps 3 and 4. I would suggest you try this:</p> <pre><code>NodeP recursiveReverseList(NodeP first){ if(first == NULL) return NULL; // list does not exist. if(first-&gt;next == NULL) return first; // list with only one node. NodeP rest = recursiveReverseList(first-&gt;next); // recursive call on rest. //rest-&gt;next = first; CHANGE THIS first-&gt;next-&gt;next = first; // make first next to the last node in the reversed rest. first-&gt;next = NULL; // since first is the new last..make its next NULL. //return first; CHANGE THIS return rest; // rest now points to the head of the reversed list. } </code></pre> <p><a href="http://geeksforgeeks.org/wp-content/uploads/2009/07/Linked-List-Rverse.gif" rel="nofollow noreferrer">image http://geeksforgeeks.org/wp-content/uploads/2009/07/Linked-List-Rverse.gif</a>.</p> <p><strong>EDIT:</strong></p> <p><s>PS: I've not tested this. So try it and let us know :)</s></p> <p>I've tested the above function and seems to work as expected. You can try the program here: <a href="http://ideone.com/bQXAV" rel="nofollow noreferrer">http://ideone.com/bQXAV</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload