Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created May 7, 2016 04:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jianminchen/9013539e71764a018f3745c514da9d91 to your computer and use it in GitHub Desktop.
Save jianminchen/9013539e71764a018f3745c514da9d91 to your computer and use it in GitHub Desktop.
HackerRank - delete duplicate value nodes - easy question
/*
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