Created
June 7, 2014 16:49
-
-
Save abhi1010/e2b7117c20d0f591896f to your computer and use it in GitHub Desktop.
Remove Duplicates from Linked Lir
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
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