logoNEURAL COOK
SearchAll PostsTagsNotesAuthor
logoNEURAL COOK

A posts created with Next.js and Tailwind.css

Explore

  • All posts
  • Notes
  • Snippets
  • Tags
  • RSS feed

Connect

instagramtwitterfacebookyoutubelinkedinmailgithub

© 2026 VIKI. All rights reserved.

Built with
Contents
ArraysBoyer-Moore Majority Vote AlgorithmKadane’s AlgorithmTriple Reverse algorithm aka Reversal Algorithm
Cycle detectionDijkstraDisjoint Set UnionGraphTopological SortTraversal algorithmsTree Traversal
Binary HeapBinomial HeapHeap
Cycle DetectionLinked ListSorting
Finding the majority elementRotate array by K steps
Finding If Number is PrimeGCD of two numbersPerforming ExponentiationPrime NumbersRandomising Data
3. Max Consecutive OnesHoare'S Partition SchemeLomuto Partition Scheme
DequesPriority QueueQueue
The Binary Search Master GuideThe Rotated Sorted Array Master Guide
Bubble SortCounting SortInsertion SortMerge SortPigeon Hole SortQuick SortSelection Sort
Monotonic StackMonotonic StackStackReversing an ArrayStackTower of Hanoi
Binary Tree TraversalCentre of gravityDiameter of TreeShortest Path
Algorithmic Pattern/linked list

Cycle Detection

3 min read
On this pageTortoise and Hare <a href="#head-3-41" id="head-3-41"></a>
Tortoise and Hare <a href="#head-3-41" id="head-3-41"></a>Marking Cells <a href="#head-3-37" id="head-3-37"></a>Using Hash Tables <a href="#head-3-38" id="head-3-38"></a>Loops in Doubly Linked Lists <a href="#head-3-42" id="head-3-42"></a>References&#x20;

For the following linked list

  1. How to tell whether a linked list contains such a loop ?
  2. How to find where the loop starts and break it there to “fix” the list ?

# Tortoise and Hare

It is called the tortoise-and-hare algorithm or Floyd's cycle-finding algorithm after Robert Floyd.


The algorithm starts two objects called tortoise and hare moving through the list at different speeds starting at the beginning of the list.

  • The tortoise moves one cell per step.
  • The hare moves two cells per step.

If the two meet at the same node, it indicates that there is a cycle present in the linked list.

# How we can detect the first node of the cycle ?

The following pseudocode shows the algorithm at a high level:

  1. Start the tortoise moving through the list at one cell per step.

  2. Start the hare moving through the list at two cells per step.

  3. If the hare finds a null link, then the list has no loop, so stop.

  4. Otherwise, when the hare catches the tortoise,

    Restart the hare at the beginning of the list, moving one cell per step this time.

    Continue moving the tortoise at one cell per step.

  5. When the tortoise and hare meet again, they are at the start of the loop.

  6. Leave the hare at the loop's starting point to take a well-deserved rest while the tortoise takes one more lap around the loop.

  7. When the tortoise's Next pointer gives the cell where the hare is waiting, the tortoise is at the end of the loop.

  8. To break the loop, set the tortoise's cell's Next pointer to null.\

# Marking Cells

Easiest way to tell whether a linked list has a loop is to traverse its cells, marking each as you visit it. If on a cell that is already marked, know that the list has a loop and that it starts at that point.\

Node findLoopStart(Node head) {
    Node previous = null, current = head;
    while(current != null) {
        if(current.visited) {
          // can use previous to break the loop
          return current;
        }
        previous = current;
        current = current.next;
    }
    
    return null;
}

This algorithm also requires that each cell have an added Visited field.

# Using Hash Tables

Move through the list, adding each cell to a hash table.

  • On visiting a node checks the hash table to see whether the node is already in the table.
  • If it comes to a node that is already in the hash table, the list contains a loop that starts at that node.

This algorithm obeys the restriction that we are not allowed to modify the node class, but it uses extra storage

# Loops in Doubly Linked Lists

If there is a loop, somewhere a Next pointer jumps back to an earlier part of the list

The Prev pointer in that cell points to an earlier cell, not the one that created the loop.

# References

  • Floyd's Linked List Cycle Finding Algorithm
    \
←PreviousHeapNext→Linked List

On this page

Tortoise and Hare <a href="#head-3-41" id="head-3-41"></a>Marking Cells <a href="#head-3-37" id="head-3-37"></a>Using Hash Tables <a href="#head-3-38" id="head-3-38"></a>Loops in Doubly Linked Lists <a href="#head-3-42" id="head-3-42"></a>References&#x20;