Skip to content

Instantly share code, notes, and snippets.

@sarb1208
Created August 4, 2018 15:55
Show Gist options
  • Save sarb1208/c0f91ac962f102da3046b4c2de48dc3b to your computer and use it in GitHub Desktop.
Save sarb1208/c0f91ac962f102da3046b4c2de48dc3b to your computer and use it in GitHub Desktop.
Following is reursive way of reversing a singly linked list.
/* 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