1 判断链表是否存在
2 两个指针slow、fast从头部指针出发,分别走一步、两步。
3 相遇时,fast指针回到头部指针,每次移动一步,slow指针在原地每次移动一步
4 再次相遇的位置,就是环结点的位置
class ListNode:
def __init__(self, x):
self.val = x
self.next= None
def EntryNodeOfLoopByCollections(pHead):
if pHead == None:
return None
ListNode fast = pHead.next.next
ListNode slow = pHead.next
while fast.val != slow.val:
fast = fast.next.next
slow = slow.next
fast = pHead
while fast.val != slow.val:
fast = fast.next
slow = slow.next
return fast