Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created May 9, 2016 18:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/5bdf999c91964e87788e0d010eb52e53 to your computer and use it in GitHub Desktop.
Save jianminchen/5bdf999c91964e87788e0d010eb52e53 to your computer and use it in GitHub Desktop.
HackerRank - reverse a doubly linked list
/*
Reverse a doubly linked list, input list may also be empty
Node is defined as
struct Node
{
int data;
Node *next;
Node *prev;
}
*/
Node* Reverse(Node* head)
{
if(head==NULL)
return NULL;
if(head->next ==NULL)
return head;
Node* newHead = Reverse(head->next);
head->next->next = head;
head->prev =head->next;
head->next = NULL;
return newHead;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment