Quick revision
The short answer
We use Floyd's algorithm: a slow pointer moves one node at a time and a fast pointer moves two. If the fast pointer reaches null there is no cycle; if the two pointers meet, there is one. To find where the cycle begins, we move one pointer back to the head and advance both one step at a time, and they meet exactly at the entry node. It runs in O(n) time with O(1) extra space, while a HashSet of visited nodes is simpler but needs O(n) space and must compare nodes by identity.
What to remember
In this answer, we saw two ways to detect a cycle and find its entry. A set of visited nodes, compared by identity, finds the first repeated node in O(n) time and O(n) space. Floyd's algorithm moves a slow pointer one step and a fast pointer two steps; a meeting proves a cycle, and restarting one pointer at the head and moving both one step at a time leads them to the entry, because a = m c - b. It needs O(n) time and only O(1) space, and one more lap from the meeting point gives the cycle's length.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We use Floyd's algorithm: a slow pointer moves one node at a time and a fast pointer moves two. If the fast pointer reaches null there is no cycle; if the two pointers meet, there is one. To find where the cycle begins, we move one pointer back to the head and advance both one step at a time, and they meet exactly at the entry node. It runs in O(n) time with O(1) extra space, while a HashSet of visited nodes is simpler but needs O(n) space and must compare nodes by identity.
Why this question is asked
A cycle in a linked list is the data structure version of an infinite loop: code that walks the list never finishes, and in production that looks like a thread stuck at 100% CPU. The same idea appears in real work when we follow parent links in a tree that might be corrupted, detect loops in a chain of redirects, or check a graph of references. Interviewers like Floyd's algorithm because it is short but needs a real argument to explain why the second phase finds the entry. In this answer, we will recap what a cycle is, see the simple HashSet solution, write Floyd's algorithm, prove why it finds the entry, and then measure the cycle's length.
1A Quick Recap of Cycles in a Linked List
A singly linked list is a chain of nodes where each node holds a value and a reference to the next node. Normally the last node's next is null. A cycle exists when some node's next points back to an earlier node, so following next from the head never reaches null. The node where the chain first enters the loop is the cycle entry, the node the question asks for.
We will use a small example: nodes 1 -> 2 -> 3 -> 4 -> 5 -> 6, where 6 points back to 3. The distance from the head to the entry is 2 steps, and the cycle 3 -> 4 -> 5 -> 6 -> 3 has length 4.
2The Simple Solution With a Set
The most direct answer remembers every node we visit. The first node we see twice is the entry.
Notice the IdentityHashMap-based set. A plain HashSet uses equals() and hashCode(), and if Node overrides them to compare values, two different nodes holding the same value would look like the same node and we would report a cycle that does not exist. A node's identity is what forms a cycle, so we compare with ==. The cost of this solution is O(n) extra space, one set entry per node, which matters for a list of millions of nodes.
3Floyd's Algorithm
Floyd's algorithm, also called the tortoise and the hare, uses two pointers and no extra memory. In the first phase, slow moves one step and fast moves two steps per iteration. If there is no cycle, fast reaches the end. If there is a cycle, both pointers eventually enter it, and because fast gains one node on slow at every step, it must catch up and land on the same node.
| Step | slow | fast | Note |
|---|---|---|---|
| 0 | 1 | 1 | both start at the head |
| 1 | 2 | 3 | |
| 2 | 3 | 5 | slow enters the cycle |
| 3 | 4 | 3 | fast has wrapped around |
| 4 | 5 | 5 | they meet: a cycle exists |
| phase 2, step 1 | 6 (from 5) | 2 (p from 1) | p replaces fast and starts at the head |
| phase 2, step 2 | 3 | 3 | they meet at the entry |
The loop condition checks fast and fast.next, because fast jumps two nodes and either could be null at the end of a list without a cycle. In our example, slow visits 2, 3, 4, 5 while fast visits 3, 5, 3, 5, so they meet at node 5 after four steps. Then p starts at node 1 and slow at node 5, and after two steps both are at node 3, the entry.
4Why the Second Phase Finds the Entry
Let a be the number of steps from the head to the entry, c the length of the cycle, and b the number of steps from the entry to the meeting point, measured along the cycle. When the pointers meet, slow has taken a + b steps. Fast has taken twice as many, 2(a + b), and it has gone around the cycle some whole number of times, m, more than slow. So 2(a + b) = a + b + m c, which simplifies to a + b = m c, and therefore a = m c - b.
Now read that last equation as a statement about walking. Starting at the meeting point, which is b steps past the entry, if we take m c - b steps we arrive back at the entry, because m c steps would bring us back to the meeting point and b fewer steps stop at the entry. And m c - b is exactly a, the distance from the head to the entry. In terms of positions on the cycle, a is congruent to c - b modulo c. So a pointer starting at the head and a pointer starting at the meeting point, both moving one step at a time, reach the entry after a steps, at the same moment. In our example a = 2, c = 4 and b = 2, and indeed 2 = 4 - 2.
Both phases are linear. Slow enters the cycle within a steps, and fast catches it within c more steps, and the second phase takes a steps. So the time is O(n) and the extra space is O(1): just three references.
5Follow-up questions
How would you find the length of the cycle?
After the pointers meet, I keep one of them still and move the other around the cycle one step at a time, counting steps until it comes back to the meeting node. That count is c, the cycle length, and it takes O(c) extra time. In our example the count is 4.
Why does the fast pointer move exactly two steps?
Two is the simplest choice that guarantees a meeting: fast gains exactly one node per iteration, so once both are in the cycle the gap shrinks by one each time and cannot jump over zero. With three steps, the gap changes by two per iteration and the pointers can skip past each other in some cycle lengths, taking longer to meet. The entry proof above also relies on fast having taken exactly twice slow's steps.
Would you ever use the HashSet version in production?
Yes, when memory is not a concern and clarity matters more, for example a validation step on a small structure. It also tells us the entry in one pass and is easy to review. I would still use the identity-based set, because value-based equals on nodes would give false cycles. For very long chains, Floyd's algorithm avoids allocating a set entry per node.
6Conclusion
In this answer, we saw two ways to detect a cycle and find its entry. A set of visited nodes, compared by identity, finds the first repeated node in O(n) time and O(n) space. Floyd's algorithm moves a slow pointer one step and a fast pointer two steps; a meeting proves a cycle, and restarting one pointer at the head and moving both one step at a time leads them to the entry, because a = m c - b. It needs O(n) time and only O(1) space, and one more lap from the meeting point gives the cycle's length.
Open with
I would use Floyd's algorithm. I start two pointers at the head, a slow one that moves one node per step and a fast one that moves two.
Then, if they want more
If the fast pointer reaches null, the list ends, so there is no cycle. If there is a cycle, both pointers end up inside it, and the fast one gains one node per step, so it must land on the slow one.
To find where the cycle begins, I move one pointer back to the head and advance both one step at a time; they meet at the entry. The reason is a short calculation. Say the head is a steps from the entry, the meeting point is b steps into the cycle, and the cycle has length c.
Slow walked a plus b, fast walked twice that, and the difference is a whole number of laps, so a plus b equals m times c. That means a equals m c minus b, which is exactly the distance from the meeting point forward to the entry.
So walking a steps from the head and from the meeting point arrives at the same node. It is O(n) time and O(1) space.
The simpler alternative is a set of visited nodes, which is O(n) space, and I would make it identity based with IdentityHashMap, because if Node overrides equals, two nodes with the same value would look like a cycle. To get the cycle length, I would walk once around from the meeting point and count.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Write a Java method that detects whether a singly linked list has a cycle, and returns...": https://scrivio-interview-library.netlify.app/java/coding-problems/591efdabee/