index
title: 链表中环的入口结点 date: 2019-08-21T11:00:41+08:00 draft: false categories: offer
题目
解题思路
public ListNode EntryNodeOfLoop(ListNode pHead) {
if (pHead == null || pHead.next == null) return null;
ListNode fast = pHead, slow = pHead;
while (fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if (fast == slow) break;
}
if (fast != slow) return null;
ListNode cursor = pHead;
while (cursor != fast) {
cursor = cursor.next;
fast = fast.next;
}
return cursor;
}Last updated