力扣142.环形链表II (链表)

public class Solution {
public ListNode detectCycle(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
//根据Floyd判圈法算出入口节点位置
while (slow != head) {
slow = slow.next;
head = head.next;
}
return slow;
}
}
return null;
}
}

浙公网安备 33010602011771号