Floyd’s cycle detection
a linked list If we use two pointers slow and fast, the first one moves forward one step at a time, and the second one moves forward two steps at a time. Eventually, the two pointers will meet somewhere. In the above example, they meet at number 9 (or G). G is where two-pointers fast and slow meet. E is the entrance of the loop. S is the first node of the linked list. F is the distance from the chain’s beginning to the loop’s entrance (from S to E). (eg: 1 – 3 – 5) a is the distance from the loop’s entrance to the point where two pointers meet (from E to G). (eg: 5 – 6 – 7 – 8 – 9) C is the length of the loop. (eg: the loop is 5 – 6 – 7 – 8 – 9 – 10, the length is 6 ) The total distance that the slow pointer has moved is $F + a + xC$. $x$ is the number of times the slow pointer completes the loop. The total distance that the fast pointer has moved is $F + a + yC$. Likewise, $y$ is the number of times the fast pointer completes the loop. ...