1

Now I remember reading on a stack overflow answer that the fast and slow runners will always collide in a linked list loop and that the difference in speed by 2 is arbitrary.

If the difference between the fast and slow runners is 2, then I haven't been able to come up with a counter example but if the difference between the runners is 3 then I can't seem to make the runners collide in the following example.

Suppose we had 2 runners, slow and fast, inside a linked list loop. The loop has 8 nodes, 1st node being labelled 0, 2nd being 1, and so on. Slow is at index 0 and fast is at 1, then they never collide:

slow fast 
0    1
1    4
2    7
3    2
4    5
5    0
6    3
7    6
0    1 # pattern seems to restart here

This example and the statement that the runners inside a loop will always collide are contradictory, as the fast runner can keep jumping over the slow runner.

I haven't taken a course on algorithms yet so please try to explain what I have got wrong as simply as possible.

Ammar Akhtar
  • 83
  • 1
  • 2
  • 9

0 Answers0