Detect Cycle in Linked List | Java Fresher Interview
Fast recall
Quick revision
Core answer
slow one node and fast two nodes each step. If they ever meet, the list has a cycle; if fast reaches null, it doesn't. O(n) time, O(1) space. To find the cycle's start, reset one pointer to the head after they meet and advance both one step at a time until they meet again.Whether you reach for Floyd's O(1)-space detection and can extend it to find the cycle start
Using O(n)-space HashSet, or NPE from not guarding fast.next
Add the bonus: resetting to head finds the entry point of the loop
Interview-ready guide
Interview answer
I'd use Floyd's cycle detection, the tortoise and hare.
I start two pointers at the head, then move slow one node per step and fast two nodes per step. If the list is acyclic, fast eventually reaches null and I return that there's no cycle. If there is a cycle, both pointers enter the loop, and because fast gains one position on slow every step, the gap between them shrinks to zero and they're guaranteed to meet on the same node.
That meeting is my proof of a cycle, and it's O of n time with O of one space, which beats the HashSet approach that costs O of n space. The part that really impresses interviewers is finding where the cycle begins. After the two pointers meet, I reset one of them, say slow, back to the head, and then I advance both pointers one node at a time.
The node where they meet the second time is the start of the cycle. That falls out of the distance relationship: the distance from the head to the loop entry equals the distance from the meeting point to the loop entry, going around. I won't derive the full math unless asked, but I know the procedure.
Two implementation details: I guard the loop with fast not null and fast dot next not null so I don't dereference null when stepping fast twice, and I compare node references with double equals, not their values, because duplicate values don't imply a cycle.
Detect Cycle in Linked List | Java Fresher Interview
Direct Answer
Floyd's tortoise-and-hare: move slow one node and fast two nodes each step. If they ever meet, the list has a cycle; if fast reaches null, it doesn't. O(n) time, O(1) space. To find the cycle's start, reset one pointer to the head after they meet and advance both one step at a time until they meet again.
Deep Dive & Architecture
Floyd's cycle detection uses two references from the head. slow moves one node per step and fast moves two. If fast reaches null, the list ends and has no cycle. If the references become equal, a cycle exists.
Worked example: 1 -> 2 -> 3 -> 4, with 4.next = 2
After one round, slow is at 2 and fast at 3. After two, slow is at 3 and fast is back at 2. After three, both are at 4. Neither pointer needed a visited set.
The reason a meeting is guaranteed is relative speed. Once both pointers enter a cycle of length c, fast gains one node on slow during every round. Their gap modulo c therefore changes by one until it becomes zero.
Check fast != null && fast.next != null before the two-node move. This avoids a null access on an acyclic list. Comparing node references is also essential; equal values do not mean the same node.
The method is O(n) time and O(1) extra space. To find the cycle entry, move one pointer back to the head after the meeting, then advance both one step at a time; their next meeting is the entry node.
Code Example
Node detectCycleStart(Node head) {
Node slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) { // cycle confirmed
slow = head; // reset one pointer
while (slow != fast) { slow = slow.next; fast = fast.next; }
return slow; // entry point of the loop
}
}
return null; // no cycle
}
// 1->2->3->4->5->3(back): meet inside loop, reset, both walk -> meet at 3
Common Mistakes & Pitfalls
- HashSet of visited: O(n) space when Floyd's gives O(1).
- NPE: guard
fast != null && fast.next != null. - Comparing values: compare node references.
- Skipping the reset proof: know the meeting->head reset finds the cycle start.
Check your understanding
Answer these without looking back at the article.
- How do you reverse a singly linked list in Java?
- How do you find the middle element of a linked list?
- What is a linked list and how does it differ from an array?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.