Skip to content

Instantly share code, notes, and snippets.

@abhi1010
Created June 7, 2014 16:49
Show Gist options
  • Save abhi1010/e2b7117c20d0f591896f to your computer and use it in GitHub Desktop.
Save abhi1010/e2b7117c20d0f591896f to your computer and use it in GitHub Desktop.
Remove Duplicates from Linked Lir
void removeDuplicates(Node* node)
{
Node* n = node;
std::set<Node*, Node_Comparable> nodeSet;
nodeSet.insert(n);
while (n->next != NULL)
{
std::pair<std::set<Node*>::iterator, bool> ret = nodeSet.insert(n->next);
if (ret.second) // element not there
{
// Good enough.
LOG ("removeDuplicates: Added " << n->next->data);
}
else // already exists. Let's remove this
{
LOG ("removeDuplicates: Found duplicate......... " << n->next->data);
Node* nextNext = n->next->next;
delete n->next;
n->next = nextNext;
}
n = n->next;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment