Linked List Cycle II
Question
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Note: Do not modify the linked list.
Follow up: Can you solve it without using extra space?
Solution
會回來 update 這題,是因為遇到了這題 287. Find the Duplicate Number。原則上,遇到要判斷環的問題,就可以使用快慢指針,如果快的可以遇到慢的,那就代表其中有環。這題進階一點,問的是環的起點在哪?
說穿了這題是數學題,我們先假設慢指針走了 A 步之後遇到環的起點,接著走了 B 步後被快指針遇到。因此,由於快指針是慢指針的兩倍快,所以可以直接知道快指針走了 2A + 2B 才追到慢指針。而剛好,快指針是比慢指針多走了那一圈 cycle,所以可以把算式寫成 A + B + N = 2A + 2B,因此 A + B = N
此時你發現,從開頭到環的起點也是走了 A 步,而慢指針繼續走 A 步也剛好走完一圈(因為慢指針走進環後只走了 B 步就被遇到),因此只要新建一個慢指針從開頭跟環裡面的慢指針一起走,再次相遇就代表是環的起點
public ListNode detectCycle(ListNode head) {
if (head == null || head.next == null) {
return null;
}
ListNode slow = head.next;
ListNode fast = head.next.next;
while (fast != null && fast.next != null) {
if (slow == fast) {
ListNode slow2 = head;
while (slow2 != slow) {
slow = slow.next;
slow2 = slow2.next;
}
return slow;
}
slow = slow.next;
fast = fast.next.next;
}
return null;
}