cIrcular linked list(floyds cycle removal)


i am getting run error plz help me out with the code

Hi Eshanika
You haven’t created the loop in the list. For example if input is 1 2 3 4 1 -1 than it should be
1->2->3->4->1 where 4->1 is loop
but you have created it like 1->2->3->4->1->NULL
therefore when you run first while loop in floydcycle algo, fast = 1 and slow=3
in second while loop fast becomes NULL and we cannot use NULL->data , it gives run error.

Hope it helps :slight_smile:

Ok so how should i modify the code

1.Detect Loop using Floyd’s Cycle detection algorithm and get the pointer to a loop node.
2.Count the number of nodes in loop. Let the count be k.
3.Fix one pointer to the head and another to a kth node from the head.
4.Move both pointers at the same pace, they will meet at loop starting node.
5.Get a pointer to the last node of the loop and make next of it as NULL.

hope it helps:)
please give a like is satisfied

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.