We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
If there is no loop in the linked list then fast.next will become null after reaching the end of linked list and the while loop will exit without ever marking the flag as 1.
And if there is a loop it will catch up with slow as it is moving twice as fast and as soon as fast will be equal to slow the flag will be marked as 1 and finally the while loop will exit.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Cycle Detection
You are viewing a single comment's thread. Return to all comments →
If there is no loop in the linked list then fast.next will become null after reaching the end of linked list and the while loop will exit without ever marking the flag as 1. And if there is a loop it will catch up with slow as it is moving twice as fast and as soon as fast will be equal to slow the flag will be marked as 1 and finally the while loop will exit.