Linked List Cycle

easyLinked Listfast/slow pointers (Floyd's cycle detection)B75Solve on LeetCode#141

Part of Practice: Core Surgery

Given the head of a linked list, decide whether the list contains a cycle — that is, whether following next pointers forever eventually revisits a node instead of reaching null. Return true if a cycle exists, false otherwise.

The list can have up to 10^4 nodes and may be empty. The follow-up (and the expected interview answer) is to solve it with O(1) memory — no set of visited nodes.

Examples

Input:
head = [3, 2, 0, -4], where the last node points back to the second node
Output:
true

Following next pointers loops forever through 2 -> 0 -> -4 -> 2 ...

Input:
head = [1, 2], where the last node points back to the first
Output:
true
Input:
head = [1], with no back-pointer
Output:
false

Walking from the single node immediately reaches null.

Hints

One at a time — struggle first, peek second.

Your thinking space — verify on LeetCode, then mark it.