Detect cycle in a linked list
Course: Linked List - chapter 4
Q - https://leetcode.com/problems/linked-list-cycle/description/
Solution
Section titled “Solution”To detect a cycle in a linked list, we use the fast and slow pointer approach. The idea is to move one pointer at 2x speed and another pointer at 1x speed. If there is a cycle, they will reach the same node at same time again. This is called Floyd cycle detection algorithm.
Now if the question also tells to remove the loop if present, we need to find the starting position of the loop.

Total distance travelled by slow pointer: m + c1*n + k, where c1 is a positive constant
Total distance travelled by fast pointer: m + c2*n + k, where c2 is a positive constant
As fast pointer is 2x fast than slow pointer, it has covered 2x distance
So,
m + c2n + k = 2 (m + c1*n + k)
or, (c2 - 2c1) * n = m + k
or, c*n = m + k, where c is a positive constant
or, m = c*n - k ———————- (1)
Now, if we reset the slow pointer to head and fast pointer stays in current position (i.e at intersection point), and both pointers move 1 unit distance at a time, in m units slow pointer would have travelled ‘m’ units and reach the starting of the loop. The fast pointer also travels ‘m’ units but from equation (1) it is equal to c*n - k. As the pointer already started k units away from c, it will also reach the beginning of the loop.
So the pointers intersect again at the beginning of the loop.