Created
May 7, 2016 04:51
-
-
Save jianminchen/9013539e71764a018f3745c514da9d91 to your computer and use it in GitHub Desktop.
HackerRank - delete duplicate value nodes - easy question
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) | |
{ | |
// This is a "method-only" submission. | |
// You only need to complete this method. | |
if(head==NULL) return NULL; | |
Node* cur = head; | |
// 1->1->1->2 | |
while(cur!=NULL) | |
{ | |
Node* copy = cur; | |
int count = 0; | |
if(cur->next != NULL && cur->data == cur->next->data) | |
{ | |
count++; | |
cur->next = cur->next->next; | |
} | |
if(count==0) | |
cur = cur->next; | |
} | |
return head; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment