How to determine if a linked list has a cycle using only two memory locations
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Introduction
Cycle detection in linked lists can be done in linear time while using only two pointer variables. The standard solution is Floyd’s tortoise-and-hare algorithm, where one pointer moves one step and the other moves two steps. If a cycle exists, they eventually meet; if not, the fast pointer reaches null.
Core Sections
Why Two Pointers Are Enough
A linked list with no cycle has a natural end. A fast pointer moving twice as quickly will always hit that end first. In a cyclic list, the fast pointer repeatedly laps the slow pointer inside the cycle, so the distance between them eventually becomes zero.
This gives:
- time complexity linear in node count
- constant additional memory
- no need for hash sets or node marking
The algorithm is both memory-efficient and robust.
Baseline Implementation in Python
This function only stores slow and fast pointers plus loop-local state.
Building Test Cases
Create both acyclic and cyclic lists to verify behavior.
Testing both shapes prevents false confidence from single-case validation.
Finding the Cycle Entry Point
After detection, you can locate the exact node where cycle begins without extra memory. Reset one pointer to head, keep the other at meeting point, then move both one step at a time.
This extension is useful in debugging corrupted pointer structures.
Why the Meeting Proof Works
Inside the cycle, suppose cycle length is k. Each loop iteration increases fast-minus-slow progress by one node modulo k, because fast takes two steps and slow takes one. Modular arithmetic guarantees eventual equality modulo k, so they meet.
This proof explains why algorithm does not miss cycles regardless of where cycle starts.
Edge Cases to Handle
Critical edge conditions:
- empty list
- single node with no cycle
- single node pointing to itself
- long acyclic prefix before cycle
The loop condition fast and fast.next handles most of these safely.
Alternative Method and Tradeoff
Using a set of visited nodes is simpler to reason about but uses linear extra memory.
Choose this when readability matters more than memory constraints.
Common Pitfalls
- Advancing fast pointer incorrectly and causing attribute errors on null nodes.
- Comparing node values instead of node identity.
- Forgetting to test self-loop single-node lists.
- Assuming detection function also returns entry node automatically.
- Accidentally mutating list while traversing and invalidating logic.
Summary
- Floyd’s two-pointer method detects cycles using constant extra memory.
- Fast pointer reaching null means list is acyclic.
- Pointer meeting means cycle exists.
- A second phase can locate cycle entry node without extra memory.
- Edge-case tests are essential for robust linked-list implementations.
Related reading
- How to determine if a list is subset of another list?
- How to determine if a sequence is bitonic?
- How to determine if binary tree is balanced?
- how to determine maximum route cost in a n high numeric pyramid
- How to determine whether a binary tree is complete?
- How to directly initialize a HashMap (in a literal way)?
- How to determine memory and time complexity of an algorithm?
- How to determine simplex time complexity ie Max flow

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 courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.