I'm trying to write a method to delete duplicate nodes from a sorted linkedlist. If the method gets the input linkedlist : 1->1->2->2->3->3 it should make the linkedlist like 1->2->3 . but the problem is it returns 1->1->2->3 that means the first duplicate element is not determined ! here is my code :
void removeDuplicates(Node head)
{
Node* Current = &head;
while(Current != NULL && Current->next != NULL)
{
while(Current->next != NULL && Current->next->info == Current->info)
Current->next = Current->next->next;
Current=Current->next;
}
}
whole code added : https://paste.ubuntu.com/p/hstGyDJrkN/
the_list.erase(std::unique(the_list.begin(), the_list.end()), the_list.end());would be the usual solution to "Removing duplicate nodes from a sorted linkedlist". See std::uniquestd::uniquecould work, if the node is passed by reference instead of pointer and is provided with additional increment and comparison operators - and probably assignment, too, preferably move-assignment in given case...