Detect a cycle with two pointers
Floyd's tortoise and hare: find a loop in a linked list with no extra memory.
By devskills editorial team · Last updated
The problem
Given the head of a linked list, return true if some node's Next points back to an earlier node, so walking it would never end.
Brute force first
Remember every node you visit in a map[*Node]bool. If you reach a node already in the map, there is a cycle; if you reach nil, there is not.
O(n) time, O(n) space. Correct and easy to explain; the follow-up is always "now do it in O(1) space".
The approach
The easy answer is a map[*Node]bool of visited nodes: O(n) time, O(n) space. Say it first.
Then the better one. Move slow one step and fast two steps. With no loop, fast reaches nil. With a loop, fast gains one node per step on slow and must land on it. O(n) time, O(1) space.
package mainimport "fmt"type Node struct { Val int Next *Node}// hasCycle is Floyd's tortoise and hare: slow moves one step, fast moves two.// If there is a loop, fast laps slow and they meet. If not, fast hits nil.func hasCycle(head *Node) bool { slow, fast := head, head for fast != nil && fast.Next != nil { slow = slow.Next fast = fast.Next.Next if slow == fast { // pointer comparison: the same node, not the same value return true } } return false}func main() { a := &Node{Val: 1} b := &Node{Val: 2} c := &Node{Val: 3} a.Next, b.Next = b, c fmt.Println(hasCycle(a)) // false c.Next = b // 3 points back to 2 fmt.Println(hasCycle(a)) // true fmt.Println(hasCycle(nil)) // false}Dry run
1 → 2 → 3 → 4, and 4 points back to 2. Slow moves 1, fast moves 2:
| step | slow | fast | same node? |
|---|---|---|---|
| start | 1 | 1 | (not checked) |
| 1 | 2 | 3 | no |
| 2 | 3 | 2 (3 → 4 → 2) | no |
| 3 | 4 | 4 (2 → 3 → 4) | yes, return true |
Go-specific gotchas
- Check `fast != nil && fast.Next != nil` before
fast.Next.Next. Both are needed, or you get a nil pointer panic on even-length lists. - `slow == fast` compares pointers, which means "the same node". Comparing
slow.Val == fast.Valis a bug when values repeat. - Pointers are valid map keys, so
map[*Node]boolworks for the simple version.
Why they must meet, and how long the loop is
Once both pointers are inside the loop, fast closes the gap on slow by exactly one node per step. A gap that shrinks by one each step cannot jump over zero, so they meet, and they meet before slow finishes one lap. That is where the O(n) bound comes from.
Once they have met you can measure the loop: keep one pointer still and walk the other round until it comes back, counting steps. Returning the meeting node rather than a bool makes both of these follow-ups easy.
package mainimport "fmt"type Node struct { Val int Next *Node}// meet returns where slow and fast meet, or nil if there is no cycle.func meet(head *Node) *Node { slow, fast := head, head for fast != nil && fast.Next != nil { slow, fast = slow.Next, fast.Next.Next if slow == fast { return slow } } return nil}// cycleLength walks once round the loop from the meeting point.func cycleLength(m *Node) int { n := 1 for p := m.Next; p != m; p = p.Next { n++ } return n}func main() { // 1 → 2 → 3 → 4 → 5 → 6, and 6 points back to 3 nodes := make([]*Node, 6) for i := range nodes { nodes[i] = &Node{Val: i + 1} } for i := 0; i < 5; i++ { nodes[i].Next = nodes[i+1] } nodes[5].Next = nodes[2] m := meet(nodes[0]) fmt.Println("cycle:", m != nil, "meet at:", m.Val, "length:", cycleLength(m)) fmt.Println("no cycle:", meet(&Node{Val: 1, Next: &Node{Val: 2}}) == nil)}Complexity
| Approach | Time | Extra space |
|---|---|---|
map[*Node]bool of visited nodes | O(n) | O(n), one map entry per node |
| Floyd's slow and fast pointers | O(n) | O(1) |
| Cycle length, after they meet | O(loop length) | O(1) |
Pitfalls
- Starting `fast` one node ahead (
fast := head.Next) also works, but changes where they meet; mixing the two styles is how off-by-one bugs get in. - Checking `slow == fast` before the first move returns true immediately, because both start at
head. - The map version allocates. In Go a map grows as you insert, so the "simple" version also costs allocations and GC work on a long list. The hidden tests on this page check that your solution allocates nothing.
- Write Floyd's cycle check with slow and fast pointers.
- Explain why both fast and fast.Next must be checked for nil.
- Predict where slow and fast meet on a small cyclic list.
- Map of visited nodes first, then two pointers for O(1) space.
- Guard both
fastandfast.Next. - Compare nodes, not values.
hasCycle(head *Node) bool that reports whether the list loops back on itself, using Floyd's slow and fast pointers in O(1) extra space (the hidden tests check that it allocates nothing, so no "visited" map). Example: 1 → 2 → 3 → back to 2 → true.Tab indents. Press Esc, then Tab, to leave the editor.
- 1The main task: Return the node where the cycle starts. Hint: after they meet, move one pointer back to the head and step both by one.
- 2Dry run Floyd on
1 → 2 → 3with no cycle. Why does the loop stop, and after how many steps? - 3Find the length of the cycle, once you know there is one.