Linked list loop detection algorithm
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Linked lists are a fundamental data structure used in computer science for storing a collection of elements. Unlike arrays, linked lists store elements not in contiguous memory locations but as nodes with pointers connecting them. This flexible structure allows for efficient insertion and deletion of elements. However, one common problem encountered with linked lists is the presence of loops, where nodes in the list point back to previous nodes, creating infinite cycles.
In this article, we will delve into loop detection algorithms for linked lists, concentrating on Floyd’s Cycle Detection Algorithm, also known as the Tortoise and Hare algorithm. It's a popular and efficient approach to detect loops in linked lists.
Overview of Linked Lists
A linked list consists of nodes where each node contains:
- Data
- A pointer/reference to the next node in the sequence
Types of Linked Lists
- Singly Linked List
- Each node points to the next node in the sequence.
- Doubly Linked List
- Each node points to both the next node and the previous node.
- Circular Linked List
- The last node points back to the first node.
Loop Detection in Linked Lists
Loop detection is crucial as a loop can result in infinite traversal, thus causing a program to hang.
Floyd's Cycle Detection Algorithm
Floyd’s Cycle Detection Algorithm is a two-pointer technique that is both time-efficient and space-efficient.
Algorithm Explanation
- Initialize Two Pointers:
- `slow` pointer: Moves one step at a time.
- `fast` pointer: Moves two steps at a time.
- Traversal: Move both pointers:
- The `slow` pointer traverses the nodes one by one.
- The `fast` pointer traverses the nodes two at a time.
- Detection:
- If there is no loop, the `fast` pointer will reach the end of the list (i.e., a `null` reference).
- If there is a loop, the `slow` and `fast` pointers will eventually meet within the loop.
- Determine Loop Start (optional):
- Reset one of the pointers to the start of the linked list.
- Move both pointers at one step until they meet again; the meeting point is the start of the loop.
Implementation Example
Here's a simple implementation in Python:
- Time Complexity: , where is the number of nodes. Each node is visited at most twice.
- Space Complexity: , as no additional data structures are used.
- Keep track of visited nodes in a hash table.
- If a node is encountered again, a loop is detected.
- Time Complexity:
- Space Complexity: due to extra space used for hashing.
- Modify the linked list to mark nodes as visited by changing pointers or inserting a special data field.
- This is not always feasible or recommended due to potential modifications to the original data.
Related reading
- LINQ Aggregate algorithm explained
- List of all classification algorithms
- List of all classification algorithms
- List of Big-O for PHP functions
- Linked list vs Array in Javascript
- LinkedBlockingQueue vs ConcurrentLinkedQueue
- Listing all permutations of a string/integer
- Locale based sort in Javascript, sort accented letters and other variants in a predefined way

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.