Fast and Slow Pointers

DSA · Linked Lists

Theory

The fast and slow pointer technique (Floyd's tortoise and hare) moves one pointer one step at a time and another two steps at a time. If the list has a cycle, the fast pointer eventually laps the slow one and they meet; if not, the fast pointer reaches the end. When the fast pointer reaches the end of a list without a cycle, the slow pointer is at the middle.

Why we use it

It detects cycles and finds middles in O(n) time with O(1) extra space, without storing visited nodes.

When to use it

Use it for cycle detection, finding the middle node, palindrome linked lists, and finding the start of a cycle.

How it works

  1. slow = head, fast = head
  2. fast and fast.next exist? Yes: slow moves 1, fast moves 2. No: No cycle (fast reached the end).
  3. slow == fast? Yes: Cycle detected. No: Continue moving.
  4. Done

Syntax

slow = fast = head
while fast and fast.next:
    slow = slow.next
    fast = fast.next.next
    if slow is fast: cycle found
# no cycle: slow is the middle

Example

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

nodes = [Node(i) for i in range(1, 8)]
for a, b in zip(nodes, nodes[1:]):
    a.next = b

slow = fast = nodes[0]
while fast and fast.next:
    slow = slow.next
    fast = fast.next.next
print("Middle node:", slow.val)

nodes[-1].next = nodes[2]          # create a cycle
slow = fast = nodes[0]
has_cycle = False
while fast and fast.next:
    slow, fast = slow.next, fast.next.next
    if slow is fast:
        has_cycle = True
        break
print("Cycle detected:", has_cycle)

Key points

DSA topics

Introduction to DSA

Arrays and Searching

Sorting Algorithms

Linked Lists

Stacks and Queues

Hashing

Trees

Heaps

Graphs

Algorithm Techniques

Classic Algorithms

Dynamic Programming

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Data Structures & Algorithms

The complete DSA roadmap: theory, why and when to use every structure and algorithm, Big O complexity, and Python code you can run right here in the browser.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice DSA Online 251 problems across 13 patterns, with hidden tests and step-by-step optimal solutions. Start practising →
Section {{ sec.num }}

{{ sec.label }}

DSA  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ current.why }}

When to use it

{{ current.when }}

Flowchart — how {{ current.name }} works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Step-by-step visualisation
{{ cell.v }}
{{ cell.i }} {{ cell.tag }}
Step {{ vizStepLabel }}: {{ vizNote }}
■ comparing ■ swapping / moving ■ done / found ■ out of range
Complexity & Pseudocode
{{ current.syntax }}
Example Code
{{ current.example }}
▶ Try It Live — runs real Python 3 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic