Floyd’s tortoise algorithm relies on the fact that if both pointers are moving at a different pace, the gap between them will keep on increasing to a limit, after which it’ll be reset to zero if a cycle exists.

Just like if you and your friend are driving in a circle with your friend moving twice at fast as you, You both will reach the starting point again together. It’s just that you will reach there after one revolution and your friend will be there after two revolutions.

Pingback: FLOYDS TORTOISE AND HARE ALGORITHM FIND LOOPS IN LINKED LIST - MadGhosts

Neat algorithm!

Typo on line 6: here->next should be hare->next.

And the algorithm as-is always returns True for lists greater than length 1 because hare and tortoise both point to head to start (making it so the hare “caught” the tortoise immediately). Did you mean to:

tortoise = head

hare = head->next

to start?