# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def EntryNodeOfLoop(self, pHead):
if not pHead or not pHead.next:
return None
slow = pHead
fast = pHead
# 这里这么写会有一个case过不了,暂时不知道原因,暂做标记
# {1,2},{3,4,5}
while slow != fast:
if not fast or not fast.next:
return None
slow = slow.next
fast = fast.next.next
p1 = pHead
p2 = fast
while p1 != p2:
p1 = p1.next
p2 = p2.next
return p1
# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def EntryNodeOfLoop(self, pHead):
if not pHead or not pHead.next:
return None
slow = pHead
fast = pHead
# 这样写就可以通过了,原因暂时未知,待解决
while True:
if not fast or not fast.next:
return None
slow = slow.next
fast = fast.next.next
if slow == fast:
break
p1 = pHead
p2 = fast
while p1 != p2:
p1 = p1.next
p2 = p2.next
return p1