You get head, the first node of a singly linked list. The list may be broken: instead of ending in null, its last node can point back to an earlier node, so following next from the head circles forever.
If the list loops, return the node where the loop starts: the first node you would reach a second time. If it ends in null, return null. Do not change the list.
The tests build the loop from pos, the index the last node links back to (−1 for none). Your function only receives head; the tests print the index of the node you return, or −1 for null.
Example 1
Input: head = [7, 4, 9, 1, 6], pos = 2 Output: the node at index 2 (value 9) Why: 6 links back to 9, so the loop is 9 → 1 → 6
2 more examples and the constraints
Example 2
Input: head = [8, 5], pos = 0 Output: the node at index 0 (value 8) Why: 5 links back to the head
Example 3
Input: head = [2, 6, 3], pos = -1 Output: null Why: after 3 comes null, so there is no loop
Constraints
- 0 ≤ number of nodes ≤ 10,000
- −100,000 ≤ node value ≤ 100,000
- pos is −1 or a valid index into the list
- Follow-up: use O(1) extra memory
Your turn first
Have a go on paper or in your own editor, then reveal the approaches from the simplest up.
- Remember visited nodesO(n)
- Slow and fast pointersO(n)
The idea in one sentence
The pattern, how to spot it, and the best complexity.
Side by side
| Approach | Time | Relative work for 10,000 nodes | Verdict |
|---|---|---|---|
| Remember visited nodes | O(n) | Passes; O(n) memory | |
| Slow and fast pointers | O(n) | Best; constant memory |
n = number of nodes. Both are linear; the set also pays for hashing and storing every node.
Edge cases to test
- Empty list. head is null, so return null. Check
fastbefore readingfast.next. - Loop back to the head. [8, 5] with pos 0: the head itself is the answer. The second walk returns at once if they meet on the head.
- One node pointing at itself. [1] with pos 0 is a loop of length one; return that node.
- Equal values. [3, 3, 3, 3] with pos 2 must return the node at index 2, not the first 3. Compare nodes, never values.
Common mistakes
- Returning the meeting node of the first loop. They meet somewhere in the loop, not always at its start.
- Starting the second walk with
faststill moving two steps at a time. - Comparing node values instead of node identity, so repeated values give the wrong node.
How to talk through it
- 1Clarify. Can the list be empty? Can values repeat? May I change the list? Return the node or its index?
- 2Give the set version. The first node seen twice is the entry. O(n) time and O(n) memory.
- 3Drop the memory. Fast and slow pointers find a meeting point inside the loop.
- 4Explain the second walk. Head to entry is
a; meeting point plusasteps is also the entry, so equal-speed pointers meet there. - 5Test out loud. Run the empty list, a loop back to the head and Example 1 before saying you are done.
Was this problem useful?Your vote tells us what to improve next.
Found a bug in our code, or something unclear?