Created
May 7, 2016 04:37
-
-
Save jianminchen/a13738b4ab32decb8601f29777172209 to your computer and use it in GitHub Desktop.
HackerRank - delete duplicate value nodes from a sorted linked list - most favorite solution
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
/* | |
Remove all duplicate elements from a sorted linked list | |
Node is defined as | |
struct Node | |
{ | |
int data; | |
struct Node *next; | |
} | |
*/ | |
Node* RemoveDuplicates(Node *head) | |
{ | |
Node *cur = head; | |
int last_seen = cur->data; | |
while(cur->next) { | |
if(cur->next->data == last_seen) { | |
cur->next = cur->next->next; | |
}else { | |
cur = cur->next; | |
last_seen = cur->data; | |
} | |
} | |
return head; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment