How do you reverse a linked list using recursion?
woah, wait a minute..
on line 6 you generated "newhead" and returned it unchanged on line 9 doesn't seem to work, or I am missing something.
EDIT: my mistake. I understood it wrong, seems to work for me now
R
[code:1]Node* ReverseListRecur(Node* head) { if (head->next==NULL) return head;; Node* tmp = head->next; Node* newhead = ReverseListRecur(head->next); tmp->next = head; head->next = NULL; return newhead; }[/code:1][/code]
Reverse a Linked List using recursion
woah, wait a minute..
on line 6 you generated "newhead"
and returned it unchanged on line 9
doesn't seem to work, or I am missing something.
EDIT: my mistake. I understood it wrong, seems to work for me now
R
Reverse a Linked List using recursion
[code:1]Node* ReverseListRecur(Node* head)
{
if (head->next==NULL)
return head;;
Node* tmp = head->next;
Node* newhead = ReverseListRecur(head->next);
tmp->next = head;
head->next = NULL;
return newhead;
}[/code:1][/code]