I used the code below to remove the cycle but as i print the list after removal of cycle, I get the list till the point where cycle was starting
Eg: 1 2 3 4 5 6 7 loop is 3-4-5-6-7-3
Output i get after the execution is 1-2-3 , instead of entire list.
void breakCycle(node s,nodef,node*head)
{
s=head;
node prev;
while(f!=NULL && f->next!=NULL)
{
s=s->next;
prev=f;
f=f->next;
if(f==s)
{
prev->next=NULL;
// cout<<endl<<“cycle removed”<<endl;
}
}
}
bool detectCycle(node head)
{
node *f=head;
node *s=head;
while(f!=NULL||f->next!=NULL)
{
s=s->next;
f=f->next->next;
if(f==s)
{
breakCycle(s,f,head);
return true;
}
}
return false;
}
int main()
{
node *head=NULL;
buildList(head);
// head->next->next->next->next->next->next->next=head->next->next;
//print(head);
head->next->next->next->next->next = head->next->next;
if(detectCycle(head))
{
cout<<“cycle present”<<endl;
}
else
{
cout<<“cycle absent”<<endl;
}
cout<<“list after cycle removal”<<endl;
print(head);
return 0;
}