Created
November 1, 2017 21:31
-
-
Save melastmohican/abcde2fea111dc2fba06c5ff7d9c7642 to your computer and use it in GitHub Desktop.
Reverse a linked list - https://www.hackerrank.com/challenges/reverse-a-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
| /* | |
| Reverse a linked list and return pointer to the head | |
| The input list will have at least one element | |
| Node is defined as | |
| struct Node | |
| { | |
| int data; | |
| struct Node *next; | |
| } | |
| */ | |
| Node* Reverse(Node *head) | |
| { | |
| if(head == NULL) return NULL; | |
| Node *current = head; | |
| Node *prev = NULL; | |
| Node *next = NULL; | |
| while(current != NULL) { | |
| next = current->next; | |
| current->next = prev; | |
| prev = current; | |
| current = next; | |
| } | |
| head = prev; | |
| return head; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment