We have discussed Floyd’s fast and slow pointer algorithms in Detect loop in a linked list.

The algorithm is to start two pointers, slow and fast from head of linked list. We move slow one node at a time and fast two nodes at a time. If there is a loop, then they will definitely meet. This approach works because of the following facts.

1) When slow pointer enters the loop, the fast pointer must be inside the loop. Let fast pointer be distance k from slow.

2) Now if consider movements of slow and fast pointers, we can notice that distance between them (from slow to fast) increase by one after every iteration. After one iteration (of slow = next of slow and fast = next of next of fast), distance between slow and fast becomes k+1, after two iterations, k+2, and so on. When distance becomes n, they meet because they are moving in a cycle of length n.

For example, we can see in below diagram, initial distance is 2. After one iteration, distance becomes 3, after 2 iterations, it becomes 4. After 3 iterations, it becomes 5 which is distance 0. And they meet.

**How does cycle removal algorithm work?**

Please see method 3 of Detect and Remove Loop in a Linked List

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Slow Start Backoff Algorithm for Ad-Hoc
- Fast Exponention using Bit Manipulation
- Fast inverse square root
- Exponential Squaring (Fast Modulo Multiplication)
- Fast Fourier Transformation for poynomial multiplication
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- Minimum time required to complete a work by N persons together
- Iteratively Reverse a linked list using only 2 pointers (An Interesting Method)
- Pairwise swap adjacent nodes of a linked list by changing pointers | Set 2
- Sorted insert in a doubly linked list with head and tail pointers
- Coin Change | BFS Approach
- Check if a given number is Pronic | Efficient Approach
- A Programmer's approach of looking at Array vs. Linked List
- Recursive Approach to find nth node from the end in the linked list
- Recursive approach for alternating split of Linked List
- Construct a linked list from 2D matrix (Iterative Approach)
- Extract maximum numeric value from a given string | Set 1 (General approach)
- Queue based approach for first non-repeating character in a stream
- Modify contents of Linked List - Recursive approach
- Reverse a Linked List in groups of given size (Iterative Approach)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.