How to detect a loop in a linked list?
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Detecting a loop in a linked list is a common challenge in computer science, often encountered when dealing with complex data structures. A loop in a linked list occurs when a node’s next pointer points back to a previous node in the list. This results in a cycle that can cause algorithms to enter infinite loops or other unwanted behaviors. Understanding how to identify these loops is crucial for maintaining robust and efficient applications.
Strategies to Detect a Loop
There are several techniques to detect a loop in a linked list:
- Hashing Approach: The idea is to traverse the list and store each node's reference in a hash table. If a node appears more than once in the hash table, a loop exists.
- Floyd’s Cycle-Finding Algorithm (Tortoise and Hare): This approach involves two pointers, slow and fast (also known as tortoise and hare). The slow pointer moves one step at a time, while the fast pointer moves two steps. If there is a loop, the fast pointer will eventually meet the slow pointer within the loop.
- Brent's Algorithm: Similar to Floyd's, this algorithm also uses two pointers but with distinct moving strategies, potentially offering better performance regarding iterations and space.
Floyd’s Cycle-Finding Algorithm Detailed Explanation
The algorithm known as "Floyd's Cycle-Finding" is particularly popular due to its efficiency and simplicity. Here is a step-by-step approach:
- Initialization: Start with two pointers, slow and fast, which both point to the head of the linked list.
- Movement: Move the slow pointer by one step and the fast pointer by two steps. Continue this process until either they meet or the fast pointer reaches the end of the list.
- Meeting Point: If the fast pointer reaches the end, the list doesn’t have a loop. If the slow and fast pointers meet, then a loop exists.
- Finding the Entrance of the Loop: To find the entry point of the loop, reset one pointer to the head while keeping the other at the meeting point. Move both one step at a time; the node where they meet next is the start of the loop.
Example in Code
Here's a simple implementation in Python:
Comparison of Techniques
| Technique | Space Complexity | Time Complexity | Implementation Complexity |
| Hashing | Simple | ||
| Floyd’s Cycle-Finding | Simple | ||
| Brent's Algorithm | Moderate |
Conclusion
Detecting loops in linked lists is essential for avoiding infinite loops and crashes in applications that implement linked lists. Floyd’s Cycle-Finding Algorithm offers a balance between efficiency and ease of implementation, making it highly popular. Understanding these techniques adds a valuable tool to any developer's arsenal when dealing with linked lists and other circular data structures.
Related reading
- How to detect a loop in a linked list?
- How to detect cycles in a directed graph using the iterative version of DFS?
- How to detect if a directed graph is cyclic?
- How to detect if an ellipse intersectscollides with a circle
- How to detect if the given graph has a cycle containing all of its nodes? Does the suggested algorithm have any flaws?
- How to determine if a JavaScript array contains an object with an attribute that equals a given value
- How to determine day of week by passing specific date?
- How to determine if a Delaunay triangle is internal or external?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.