Problem
Section titled “Problem”Given head, the head of a linked list, determine if the linked list has a cycle in it.
There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the next pointer. Internally, pos is used to denote the index of the node that tail’s next pointer is connected to. Note that pos is not passed as a parameter.
Return true if there is a cycle in the linked list. Otherwise, return false.
Example 1:

Input: head = [3,2,0,-4], pos = 1Output: trueExplanation: There is a cycle in the linked list, where the tail connects to the 1st node (0-indexed).Example 2:

Input: head = [1,2], pos = 0Output: trueExplanation: There is a cycle in the linked list, where the tail connects to the 0th node.Example 3:

Input: head = [1], pos = -1Output: falseExplanation: There is no cycle in the linked list.Constraints:
- The number of the nodes in the list is in the range .
-
Node.val posis-1or a valid index in the linked-list.
Follow-up: Can you solve it using (i.e. constant) memory?
Solution approach
Section titled “Solution approach”Intuition
Section titled “Intuition”Remembering every visited node in a hash set detects a revisit, but uses memory. Floyd’s tortoise and hare uses two pointers instead: slow moves one node per step, fast moves two.
- No cycle:
fastruns ahead and reaches the end of the list (nullptr), so we answerfalse. - Cycle:
fastcan never leave the loop, andsloweventually enters it too. From then on, every stepfastcloses the gap toslowby exactly one node (it moves 2,slowmoves 1). A gap that shrinks by one each step cannot jump over zero, sofastlands exactly onslowwithin one lap of the cycle.
So the two pointers meet if and only if the list has a cycle.
Algorithm
Section titled “Algorithm”- Set
slow = fast = head. - While
fastandfast->nextare notnullptr:slow = slow->next,fast = fast->next->next;- if
slow == fast, returntrue.
- Return
false.
Walkthrough
Section titled “Walkthrough”Example 1 (3 → 2 → 0 → -4 → back to 2):
| Step | slow | fast |
|---|---|---|
| start | 3 | 3 |
| 1 | 2 | 0 |
| 2 | 0 | 2 |
| 3 | -4 | -4 — they meet, return true |
To find where the cycle starts as well, see Linked List Cycle II.
Complexity
Section titled “Complexity”- Time: — without a cycle
fastreaches the end inn / 2steps; with one,slowenters the cycle withinnsteps and is caught within one more lap. - Space: — two pointers; the list is only read.
Edge cases
Section titled “Edge cases”- Empty list:
fastisnullptr, the loop never runs, andfalseis returned. - Single node (Example 3):
fast->nextisnullptr, sofalse. - Self-loop on one node:
fast->next->nextis that node, so the pointers meet after one step.
Think before you peek
Work out an approach on your own first — the explanation will still be here when you want it.
class Solution { public: bool hasCycle(ListNode *head) { ListNode *slow = head, *fast = head; while (fast && fast->next) { slow = slow->next; fast = fast->next->next; if (slow == fast) return true; } return false; }};public class Solution { public boolean hasCycle(ListNode head) { ListNode slow = head, fast = head; while (fast != null && fast.next != null) { slow = slow.next; fast = fast.next.next; if (slow == fast) return true; } return false; }}class Solution: def hasCycle(self, head: Optional[ListNode]) -> bool: slow = fast = head while fast and fast.next: slow = slow.next fast = fast.next.next if slow is fast: return True return FalseGive it a try first
Attempt the problem on LeetCode — the code will still be here.