Floyd's Cycle Detection
The Tortoise and Hare algorithm for detecting cycles in linked lists, with a mathematical proof.
There is a famous algorithm called Floyd’s Cycle Detection Algorithm aka Tortoise and Hare Algorithm. It is used to detect a cycles in a list.
The tortoise is the slow pointer and the hare as fast pointer. The hare will travel in strides of 2 while the tortoise will travel in strides of 1.
Imagine the following scenario — a linked list with a cycle.

Some notation that we will use: is the distance from the start of the list to the start of the cycle, is the length of the cycle, and is the distance from the start of the cycle to the point where the hare and tortoise meet.
If one reached the end of the list, there is no cycle. If the two meet then there are 2 scenarios that can happen:
where
Since the hare is traveling at twice the speed of the tortoise, if the two ever meet, the hare has traveled twice the distance. Therefore:
With some simple algebra
The next step is to put the hare at the beginning of the list, but this time have both the tortoise and the hare take strides of 1. The point at which the 2 meet will be the start of the cycle.
Since the tortoise is currently at when the hare travels steps:
- The hare is at
- The tortoise is at
We can rearrange the tortoise to have traveled or taking steps then an integer multiple of . Since there exists a cycle, traveling an integer multiple of around the cycle will land you at the beginning of the cycle.