I have written the code for cycle detection and removal-floyd’s algorithm but to test it I have to input a linked list with a loop. how to do it? I mean how to provide a linked list in the input such that it has a loop??
Linked list-floyd's cycle
1 Like
Hi Avanik, just create a linked list normally by adding items one after other and then create a loop by doing something like head->next->next->next->next = head;
I tried creating a loop in the buildList function but it is not working. I am not getting what I am doing wrong. Can you please check my code and tell what’s wrong with this??
link to my code
https://ide.codingblocks.com/s/51644
You detected the cycle using flyod’s algorith and now know about the slow and fast pointers and both are pointing to the same address where cycle is detected so you dont need to traverse them again instead use the fast ,slow pointers to get your linked list rid of cycle in breakCycle function.