Created
August 4, 2018 15:55
-
-
Save sarb1208/c0f91ac962f102da3046b4c2de48dc3b to your computer and use it in GitHub Desktop.
Following is reursive way of reversing a singly linked list.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Linked List Node structure | |
struct Node { | |
int data; | |
Node *next; | |
} | |
*/ | |
// Should reverse list and return new head. | |
Node* fun(Node* head,Node* prev){ | |
if(head==0) | |
return head; | |
if(head->next==0) | |
{ | |
head->next = prev; | |
return head; | |
} | |
Node* temp = fun(head->next,head); | |
head->next = prev; | |
return temp; | |
} | |
Node* reverse(Node *head) | |
{ | |
return fun(head,0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment