Linked List Cycle
Pattern: Fast and slow pointers
Floyd's method. slow moves one node and fast moves two. Without a loop, fast reaches the end. With a loop, fast gains one node per step inside it and must land on slow.
The problem
Given the head of a linked list, return whether it contains a cycle: some node whose next eventually leads back to a
node already visited. For example, 3 → 2 → 0 → -4 with the last node pointing back to 2 has a cycle, and
1 → 2 → 3 does not. Try to use O(1) extra memory.
The function name hasCycle matches LeetCode. Use the link above for the exact statement and limits. This guide's
tests build the list from its values and the index pos that the last node points back to (-1 for no cycle), as
LeetCode describes it.
Think first
- How would a set of visited nodes solve it, and what does it cost?
- What happens to two runners of different speeds on a circular track?
- Why must the gap between them close to exactly 0, not jump past it?
Hints
Brute force, and why it fails
Store every visited node in a hash set and stop at a repeat: O(n) time and O(n) memory. Floyd's method is O(n) time and O(1) memory.
The key insight
A cycle turns the walk into a circular track. A faster runner on a circular track always catches the slower one, and catching up one step at a time means they land on the same node.
Dry run
The list is 3 → 2 → 0 → -4, and -4 points back to 2. Both pointers start on 3.
The plan
slow = head, fast = head
while fast is not null and fast.next is not null:
slow = slow.next
fast = fast.next.next
if slow == fast: return true
return false
Complexity
- Time
O(n)- Space
O(1)
Before the cycle, fast covers the straight part. Inside it, the gap closes within one lap.
Edge cases
- Empty list: false.
- One node pointing to itself: true after one step.
- A long straight part before a short loop: still O(n).
I've built my logic, show the code
Java
class Solution {
public boolean hasCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next; // one step
fast = fast.next.next; // two steps
if (slow == fast) return true; // fast caught up from behind: there is a loop
}
return false; // fast reached the end
}
}C++
class Solution {
public:
bool hasCycle(ListNode* head) {
ListNode *slow = head, *fast = head;
while (fast != NULL && fast->next != NULL) {
slow = slow->next; // one step
fast = fast->next->next; // two steps
if (slow == fast) return true; // fast caught up from behind: there is a loop
}
return false; // fast reached the end
}
};Python
from lib import ListNode
class Solution:
def hasCycle(self, head: ListNode) -> bool:
slow = fast = head
while fast is not None and fast.next is not None:
slow = slow.next # one step
fast = fast.next.next # two steps
if slow is fast:
return True # fast caught up from behind: there is a loop
return False # fast reached the endCommon mistakes
- Comparing values instead of nodes, which is wrong when values repeat.
- Checking
slow == fastbefore moving, when both start on the head. - Reading
fast.next.nextwithout checkingfast.next.