Back to Data Structures & Algorithms
2026-01-227 min read

Tortoise and Hare Algorithm (Linked List cycle detection) (Data Structures & Algorithms)

Learn Tortoise and Hare Algorithm (Linked List cycle detection) (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

The Tortoise and Hare Algorithm is a fundamental data structures and algorithms concept that enables us to detect cycles in a linked list. Understanding this technique is crucial for competitive programming, debugging complex data structures, and real-world applications such as network routing. It provides an efficient way to solve the problem of cycle detection in a linked list, which can be challenging when using other methods.

Prerequisites

To fully grasp the Tortoise and Hare Algorithm, you should have a solid understanding of the following concepts:

  1. Linked lists (data structures)
  2. Basic Python programming concepts (variables, functions, loops, recursion)
  3. Pointer manipulation (for C programmers)
  4. Familiarity with linked list implementation in Python
  5. Understanding of Big O notation and time complexity analysis
  6. Knowledge of basic graph theory concepts (nodes, edges, cycles)

Core Concept

The Tortoise and Hare Algorithm is based on Aesop's fable of the hare and the tortoise. In this context, the tortoise moves one node at a time while the hare moves two nodes at a time. If there is a cycle in the linked list, they will eventually meet.

Here's an expanded explanation of the algorithm:

  1. Initialize two pointers, slow and fast, pointing to the head of the linked list.
  2. Move the slow pointer one node at a time, while the fast pointer moves two nodes at a time.
  3. If there is no cycle in the linked list, the fast pointer will eventually reach the end (None), and the slow pointer will be somewhere in the middle. In this case, the linked list has no cycle.
  4. If there is a cycle, the fast pointer will encounter it before reaching the end. At this point, the fast pointer starts moving from the beginning of the cycle, while the slow pointer continues moving along the original path. Eventually, they will meet at some point inside the cycle.

In terms of time complexity, the Tortoise and Hare Algorithm has a worst-case and average time complexity of O(n), where n is the length of the linked list (including the cycle if present). This makes it an efficient solution for cycle detection in linked lists.

Worked Example

Let's walk through a worked example to understand the Tortoise and Hare Algorithm better:

class ListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next

def has_cycle(head: ListNode) -> bool:
if not head or not head.next:
return False

slow, fast = head, head.next

while slow != fast:
if not fast or not fast.next:
return False # no cycle
slow = slow.next
fast = fast.next.next

return True # found a cycle

In this example, we define a ListNode class to represent the nodes in our linked list and a function has_cycle() that implements the Tortoise and Hare Algorithm. The function takes the head of the linked list as input and returns True if there is a cycle and False otherwise.

Common Mistakes

  1. Forgetting to check for an empty or single-node linked list: Always ensure that your implementation handles these cases properly, as they do not have cycles.
  2. Misunderstanding the algorithm's logic: Be careful with the steps of the Tortoise and Hare Algorithm. It's essential to understand why it works and how the pointers move through the linked list.
  3. Incorrect implementation: Double-check your code to ensure that it accurately implements the Tortoise and Hare Algorithm as described in this guide.
  4. Neglecting edge cases (e.g., when the cycle starts at the second node or when the cycle is very long): Make sure your implementation handles all possible scenarios.
  5. Not considering the possibility of a circularly linked list: The Tortoise and Hare Algorithm can be used to detect cycles in both cyclic and non-cyclic linked lists. Be aware that a circularly linked list may require additional handling when implementing the algorithm.

Subheadings under Common Mistakes:

  1. Handling empty or single-node linked lists
  2. Understanding the logic of the Tortoise and Hare Algorithm
  3. Incorrect implementation
  4. Edge cases (e.g., cycle starting at the second node, long cycles)
  5. Considering circularly linked lists

Practice Questions

  1. Implement the Tortoise and Hare Algorithm for detecting cycles in a circularly linked list.
  2. Modify the has_cycle() function to return the node where the cycle starts instead of just indicating whether there is a cycle or not.
  3. Write a Python function that checks if two singly-linked lists intersect and finds their intersection point if they do.
  4. What are some optimizations you can make to the Tortoise and Hare Algorithm for detecting cycles in a linked list? Discuss the trade-offs of each optimization.
  5. How would you extend the Tortoise and Hare Algorithm to work with doubly-linked lists?
  6. Implement the Floyd's Cycle-Finding Algorithm for detecting cycles in undirected graphs. Compare its time complexity, memory usage, and applicability compared to the Tortoise and Hare Algorithm.
  7. Discuss the differences between Floyd's Cycle-Finding Algorithm and Dijkstra's algorithm. When would you use each one?

FAQ

Q1: Why does the Tortoise and Hare Algorithm work?

A1: The Tortoise and Hare Algorithm works because, in a cyclic linked list, there must be some distance d such that moving d steps from any node results in reaching another node in the cycle. If we have two pointers, one moving one step at a time (the tortoise) and the other moving two steps at a time (the hare), they will eventually meet after covering a total distance of d + n, where n is the length of the loop in the cycle. Since the hare moves twice as fast as the tortoise, they will meet when the hare has covered d steps more than the tortoise. This means that the hare will be at the point where the cycle starts, and the tortoise will be somewhere in the cycle.

Q2: What if the linked list contains a loop with an odd number of nodes?

A2: In this case, when the slow pointer meets the fast pointer, they are both one node away from the beginning of the loop. To find the starting point of the loop, we can simply move one step back from where the pointers meet.

Q3: Can the Tortoise and Hare Algorithm be used for detecting cycles in other data structures like trees or graphs?

A3: No, the Tortoise and Hare Algorithm is specifically designed for cyclic linked lists. It cannot be directly applied to other data structures like trees or graphs, as they do not have loops in the same sense as linked lists. However, there are similar algorithms for detecting cycles in undirected graphs (Floyd-Warshall algorithm) and directed graphs with a single source (Depth-First Search).

Q4: What is Floyd's Cycle-Finding Algorithm? How does it differ from the Tortoise and Hare Algorithm?

A4: Floyd's Cycle-Finding Algorithm, also known as the "tortoise and the hare with a fast hare" algorithm, is an optimization of the Tortoise and Hare Algorithm for detecting cycles in undirected graphs. The main difference lies in how the pointers move through the graph:

  1. Initially, two pointers (tortoise and hare) start from different nodes and move at the same speed.
  2. When the hare moves n steps, it then moves n-1 steps at a time while the tortoise continues moving one step at a time.
  3. If there is a cycle, the two pointers will eventually meet inside the cycle.

Compared to the Tortoise and Hare Algorithm for linked lists, Floyd's Cycle-Finding Algorithm has better performance in terms of time complexity (O(n)) compared to O(n) for the linked list version. However, it requires more memory due to the need to store an additional pointer (the fast hare).

Q5: How does Floyd's Cycle-Finding Algorithm work?

A5: Floyd's Cycle-Finding Algorithm works by having two pointers, the tortoise and the hare, move through the graph. The initial distance between them is 1. When the hare moves n steps, it then moves n-1 steps at a time while the tortoise continues moving one step at a time. If there is a cycle, the two pointers will eventually meet inside the cycle. This algorithm takes advantage of the fact that the shortest cycle length in an undirected graph must be less than or equal to the diameter of the graph (the maximum distance between any two nodes). Since the hare moves faster than the tortoise, it will eventually cover a total distance greater than or equal to twice the length of the shortest cycle. When this happens, the pointers start moving closer together until they meet at the starting point of the cycle.

Q6: What is the time complexity of Floyd's Cycle-Finding Algorithm?

A6: The time complexity of Floyd's Cycle-Finding Algorithm is O(n), where n is the number of nodes in the graph. This makes it an efficient solution for cycle detection in undirected graphs.

Tortoise and Hare Algorithm (Linked List cycle detection) (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn