Skip to content

Instantly share code, notes, and snippets.

@jianminchen
Created May 7, 2016 16:59
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/b46755065e4d17c82ad793889e2c911b to your computer and use it in GitHub Desktop.
Save jianminchen/b46755065e4d17c82ad793889e2c911b to your computer and use it in GitHub Desktop.
Delete duplicate value node - excellent readable code
/*
Remove all duplicate elements from a sorted linked list
Node is defined as
struct Node
{
int data;
struct Node *next;
}
*/
Node* RemoveDuplicates(Node *head)
{
if (!head) return NULL;
Node *traversal = head;
while (traversal->next) {
if (traversal->data == traversal->next->data) {
Node *toDelete = traversal->next;
traversal->next = traversal->next->next;
delete toDelete;
} else {
traversal = traversal->next;
}
}
return head;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment