Created
June 16, 2017 10:16
-
-
Save PraveenKishore/6e573a9fc84b3914f35401e29fe3c880 to your computer and use it in GitHub Desktop.
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
| /* https://www.hackerrank.com/challenges/reverse-a-linked-list | |
| 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) | |
| { | |
| // Complete this method | |
| Node *next; | |
| Node *current = head; | |
| Node *previous = NULL; | |
| while(current != NULL) { | |
| next = current->next; | |
| current->next = previous; | |
| previous = current; | |
| current = next; | |
| } | |
| return previous; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment