mediumCodingEntryMidSenior
Detect whether a linked list has a cycle, and return the node where the cycle begins.
4 min readUpdated 2026-08-15
Answer
Short answer
Advance one pointer by one node and another by two; if they ever coincide there is a cycle. Then reset one pointer to the head and step both one node at a time, and they meet at the cycle's entry.
Was this answer helpful?
Likely follow-ups
- How would you compute the length of the cycle once you have detected it?
- Why does a step ratio of one and two guarantee a meeting, and would three and one still work?
- How would you find the middle node of a list in one pass with the same technique?
- Reverse the list iteratively and tell me which pointer you return.
Related questions
- Remove the nth node from the end of a singly linked list in a single pass.mediumAlso on linked-lists and two-pointers4 min
- Find the length of the longest substring without repeating characters.mediumAlso on two-pointers2 min
- Return every distinct triplet in the array that sums to zero.mediumAlso on two-pointers4 min
- You need the longest palindrome in a string. Where does centre-expansion cost you, and what does it cost?mediumAlso on two-pointers6 min