Wrong answer in two test case
input example:
1 2 3 1 2 3 -1
Your code will produce a wrong output for above mentioned example as you are not taking into account the data of first node in the vector v while removing the cycle.
The modified portion of code is:
void removeTheLoop(node *&head)
{
node *h=head;
vectorv;
v.push_back(head->data);
while(true)
{
int val=head->next->data;
if(find(v.begin(),v.end(),val)==v.end())
v.push_back(val);
else
{
head->next=NULL;
head=h;
break;
}
head=head->next;
}
}
Hope, this would help.
Give a like if you are satisfied.
1 Like