Removal of loop in linked list

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;

}

@ppsingh12 Hey using this algorithm, you will reach the position where cycle starts that is 3, so it will remove all the nodes of cycle, but if you just want to break the cycle, what you should do is, after finding the start you should move one pointer such that it moves one step at a time and as soon as you get the moving pointers next == stationary pointer, make the next of moving pointer NULL

okay thank you so much

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.