Created
May 9, 2016 18:48
-
-
Save jianminchen/5bdf999c91964e87788e0d010eb52e53 to your computer and use it in GitHub Desktop.
HackerRank - reverse a doubly linked list
This file contains 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
/* | |
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