描述
给一个长度为n链表,若其中包含环,请找出该链表的环的入口结点,否则,返回null。

答案:
/*
public class ListNode {
int val;
ListNode next = null;
ListNode(int val) {
this.val = val;
}
}
*/
public class Solution {
public ListNode EntryNodeOfLoop(ListNode pHead) {
ListNode fast = pHead, slow = pHead;
while(fast != null && fast.next != null) {
slow = slow.next;
fast = fast.next.next;
if(fast == slow) {
fast = pHead;
//在快慢指针相遇点开始,往后找,直到相遇,即为入口节点
while(fast != null && slow != null) {
if(fast == slow) {
return slow;
}
fast = fast.next;
slow = slow.next;
}
}
}
return null;
}
}