链接
牛客:链表中环的入口结点
LeetCode:142. 环形链表 II
思路
代码
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead) {
ListNode fast = pHead;
ListNode low = pHead;
while (fast != null && fast.next != null) {
fast = fast.next.next;
low = low.next;
if (fast == low)
break;
}
if (fast == null || fast.next == null)
return null;
low = pHead;
while (fast != low) {
fast = fast.next;
low = low.next;
}
return low;
}
}