Break The Cycle (Floyd's Cycle)

Hi sir,
I tried to implement a function to break the cycle. Can you please check it out and correct me as well as give me the feedback?

Thanks a lot.

My code: http://cpp.sh/7ft6e7

1 Like

You code is correct.
You can avoid the last loop traversal in line 14-15 if you maintain a previous node pointer for the fast pointer in 2nd loop traversal.
Anyways, your solution is correct.

You can also go through this article https://www.geeksforgeeks.org/detect-and-remove-loop-in-a-linked-list/

1 Like

Thanks @pratyush63 a lot

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.