142. Linked List Cycle II
寻找cycle的开始节点。相比快慢指针另外多加了一个指针entry,用来从头寻找cycle入口
public ListNode detectCycle(ListNode head) {
ListNode entry = head,
fast = head,
slow = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
while (entry != slow) {
entry = entry.next;
slow = slow.next;
}
return entry;
}
}
return null;
}