如图所示
代码
public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode fast = head, slow = head;
while(slow != null) {
fast = fast.next;
slow = slow.next;
if(slow == null || slow.next == null) return null;
slow = slow.next;
if(slow == fast) break;
}
slow = head;
while(slow != fast) {
slow = slow.next;
fast = fast.next;
}
return slow;
}
}