Linked List Recursive Reverse

Can’t we do it by changing the code of iterative method to recursive ??

@ashwani225 there can be multiple ways to solve the same problem, you can use the one you are comfortable with :slight_smile:

Please explain me recursive part. 1) node* smallhead=reverseRec(head->next); 2) node* c=head; 3)c->next->next=c; 4)c->next=NULL; 5)return smallhead; Suppose there are three nodes 1->2->3 while performing recursive part nodehead=3 but after that it can be replaced by 2 so how it goes to Current ??

hi @ashwani225 can you please share the code by saving it on ide.codingblocks.com first? It is difficult to read and analyse code this way.

@ashwani225 in the recursive case, we break the problem into smaller problems and assume that recursion will take care of the smaller problem for us, we dont have to think about it.
node* smallhead=reverseRec(head->next); in this line, we call the function on the rest of the linked list and assume that we get a reversed linked list back. Now the only work for us to do is to put the current head in its correct position. and the rest of the code does that only. You can dry run the remaining code if you have any confusion regarding it.
Please mark the doubt as resolved if you dont have any further queries :slight_smile:

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.