Linked List Cycle II
ID: 142; medium
Last updated
Was this helpful?
ID: 142; medium
Last updated
Was this helpful?
Let x
be the distance between the head
and the start of the cycle, y
be the distance between the start of the cycle to the first meeting point, z
be the distance between the first meeting point to the start of the cycle. It is clear that y + z = n
, where n
is the length of the cycle. Consider the following equations for their first meet:
Because they meet at the first meeting point, their time spent traveling should be the same. Setting the two t's equal to each other, we have x = z
.
Using this result, we have either one of them start at the head
of the list and the other continue at their first meeting point (with the same speed now). They will eventually meet at the start of the cycle thanks to the fact x = z
.
We first use the same algorithm (modified to return the first meeting point) for . If there is no cycle, we are done and return null. If there is a cycle, we store the first meeting point. From this first meeting point, we set the speed of both the tortoise and the hare to moving by 1 each time. Eventually, they will meet again at the start of the cycle. A quick proof is as follows: