Solution #1: If we are allowed to use extra space, iterate through the list storing each node's data in a hash table. Now, if a node's data is already present in the hash table then it's a duplicate, so remove it.
Solution #2: Iterate over the list, checking all previous elements for duplication. If a duplicate is found, remove it. Here is the code,
Solution #2: Iterate over the list, checking all previous elements for duplication. If a duplicate is found, remove it. Here is the code,
void removeDup(node* lptr) { node* curr = NULL; node* iter = NULL; node* prev = NULL; curr = lptr->next; iter = lptr; prev = lptr; while(curr != NULL) { while(iter != curr) { if(iter->data == curr->data) { prev->next = curr->next; free(curr); curr = prev; break; } iter = iter->next; } iter = lptr; prev = curr; curr = curr->next; } }
No comments:
Post a Comment