链表中环的入口结点
首先是用快慢指针使两指针相遇,之后使快指针重新指向头节点,快慢指针均以一个单位向后移动,直至再次相遇,即为交点。
public ListNode EntryNodeOfLoop(ListNode pHead) {
if(pHead==null||pHead.next==null) return null;
ListNode fast=pHead.next.next;
ListNode slow=pHead.next;
while (fast!=slow){
if(fast==null||fast.next==null) return null;
fast=fast.next.next;
slow=slow.next;
}
fast=pHead;
while (fast!=slow){
fast=fast.next;
slow=slow.next;
}
return fast;
}