I have a linkedList, I wanted to check if there are 3 nodes in a row that have the same data/value, if so i want to remove them from the list.
ex. 1, 2, 3, 4, 4, 4, 7, 5; // here from index 3-5 have same value, then the new list will be new list: 1, 2, 3, 7, 5
this is my code, I just got confused on how to keep track of the previous 2 values so I can campare them with the node that the current pointer pointing to.
//assume the rest of my code works fine
void Remove_ThreeDuplicates()
{
Node *current= head;
Node *prev = current;
Node *prev_prev = prev;
//keep track of identical found
int i = 0;
while(current->next != NULL)
{
prev_prev =head_ptr;
prev = prev_prev->Next;
current = prev->Next;
//checks if nodes are identical
if(prev_prev->data == prev->data && prev->data == current->data)
{
//remove the 3 identical nodes
prev_prev = current->next;
i++;
}
}
cout<<i<<" three pairs of identical node found\n"l;
}
Aucun commentaire:
Enregistrer un commentaire