Problem Statement

Linked List Cycle

A linked list is a chain of nodes. Each node holds a value and a pointer called next that points to the node after it. The last node usually points to nothing (None), which means the chain has ended. You are given head, the first node in the chain. Your job is to figure out if the chain has a cycle, which means a node whose next pointer loops back to an earlier node. If a cycle exists, following next over and over never ends, you just keep going in circles forever.

easyLinked ListLinked ListTime: O(n) · Space: O(1)

Signals to notice

detect loop in linked listfast and slow pointerscycle detection

Brute force first

Store visited nodes in a set. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.

The key insight

Floyd's tortoise and hare: slow moves 1 step, fast moves 2. The goal is not to be clever for its own sake, but to remember the one relationship that keeps the solution grounded as you move forward.

Trace it on head = [3,2,0,-4], pos = 1 (tail -4 links back to node 2)

init: slow=node(3), fast=node(3)
iter1: slow=node(2), fast=node(0); slow!=fast -> continue
iter2: slow=node(0), fast=node(2) (fast wrapped: -4->2); slow!=fast -> continue
iter3: slow=node(-4), fast=node(-4) (slow:0->-4, fast:2->0->-4); slow==fast -> return True
answer: True

What must stay true

If there's a cycle, the fast pointer will eventually catch the slow pointer from behind. If that remains true after every update, the rest of the reasoning has a stable place to stand.

Shape of the loop

slow = head; fast = head
while fast and fast.next:
    slow = slow.next         # tortoise: 1 step
    fast = fast.next.next     # hare: 2 steps
    if slow == fast: return True
return False

Pseudocode only — the full worked solution lives in the Solution tab.

Easy way to go wrong

Not checking for null — fast or fast.next could be null in a non-cyclic list. The fix is usually to return to the meaning of each move, not just the steps themselves.

Linked List Pattern