The 30 second answer
I first find a meeting point with the slow and fast pointers, as in the cycle-detection problem. Then I put one pointer back at the head and walk both one step at a time; they meet at the start of the cycle. If the fast pointer never meets the slow pointer, I return null. This problem is named in a public Microsoft interview report.
Problem Given the head of a linked list, return the node where a cycle begins, or null if there is no cycle. The answer must be the node itself, not only its value. Original: LeetCode 142: Linked List Cycle II.
Understanding the prompt Detecting a cycle is not enough; you need the first node of the loop. You should not modify node values, and a set of seen nodes is a correct O(n)-space answer you can offer first.
Pattern recognition Fast and slow pointers, plus the meeting-point identity: the distance from head to the cycle start equals the distance from the meeting point to the cycle start, walking forward.
Brute force A HashSet of seen nodes: the first repeated reference is the start. O(n) space.
Optimal Run the usual slow/fast loop. If they never meet, return null. When they meet, leave slow there and set a third pointer p at head. Advance p and slow one step at a time; they meet at the cycle start. You can prove this with the usual distances: if the stem has length a and they meet b steps into a cycle of length c, then a = k*c - b, so walking a from head and from the meeting point lands on the start.
Alternatives After finding a meeting point, count the cycle length, then keep two pointers c apart from the head and walk them together; they meet at the start. The reset-to-head version is shorter.
Java solution
public class LinkedListCycleII {
static class ListNode {
int val;
ListNode next;
ListNode(int val) { this.val = val; }
ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
static ListNode list(int... a) {
var dummy = new ListNode(0);
var t = dummy;
for (int x : a) {
t.next = new ListNode(x);
t = t.next;
}
return dummy.next;
}
static String str(ListNode h) {
var sb = new StringBuilder("[");
for (ListNode p = h; p != null; p = p.next) {
if (sb.length() > 1) sb.append(", ");
sb.append(p.val);
}
return sb.append("]").toString();
}
static ListNode detectCycle(ListNode head) {
ListNode slow = head, fast = head;
while (fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (slow == fast) {
ListNode p = head;
while (p != slow) {
p = p.next;
slow = slow.next;
}
return p;
}
}
return null;
}
public static void main(String[] args) {
var a = list(3, 2, 0, -4);
a.next.next.next.next = a.next; // cycle starts at 2
System.out.println(detectCycle(a).val); // 2
var b = list(1, 2);
b.next.next = b;
System.out.println(detectCycle(b).val); // 1
System.out.println(detectCycle(list(1)) == null); // true
}
}Complexity O(n) time, O(1) extra space.
Edge cases
- Official-style samples: 3 -> 2 -> 0 -> -4, tail linked to 2, returns the node 2; 1 -> 2 linked back to 1 returns the node 1; a single node with no cycle returns null.
Follow-ups
- The boolean version is Linked List Cycle.
- Interview follow-ups reported around this problem include the cycle length and how you would break the cycle.
Tests A list 3,2,0,-4 with the tail linked to 2 returns the node of value 2. A two-node cycle starting at 1 returns 1. A single node with no cycle returns null.
How to say it in the interview "I find a meeting point with slow and fast pointers. Then I put one pointer back at the head and walk both one step at a time; they meet at the start of the cycle. If fast never meets slow, there is no cycle. Constant extra space."