Linked List Cycle
Module 7 · Linked Lists
Problem
Given the head of a linked list, return whether the list contains a
cycle — some node's next pointing back to an earlier node, so
traversal never reaches null.
Examples
Example 1
Input
head = [3,2,0,-4], pos = 1OutputtrueExplanation. -4 links back to 2
Example 2
Input
head = [1,2], pos = -1OutputfalseExplanation. no cycle
Constraints
0 ≤ length ≤ 10⁴ · follow-up: O(1) memory.
Attempt it first
With the module's tools, the O(n)-space answer should come instantly (which verb from the Hash Tables module?). The O(1)-space answer is Floyd's tortoise-and-hare — you have both runners already; the work here is convincing yourself the meeting is guaranteed, not lucky. Try to argue it before opening the hints.