-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path链表中环的入口结点.py
46 lines (39 loc) · 1.06 KB
/
链表中环的入口结点.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
'''
题目描述
给一个链表,若其中包含环,请找出该链表的环的入口结点,否则,输出null。
'''
'''
分析:
时间复杂度O(n).
空间复杂度O(1),不可以使用hash
'''
class Solution():
def MeetingNode(self, pHead):
if not pHead:
return None
pSlow = pHead
pFast = pSlow.next
while pFast:
if pSlow == pFast:
return pSlow
pSlow = pSlow.next
pFast = pFast.next
if pFast:
pFast = pFast.next
def EntryNodeOfLoop(self, pHead):
meetingNode = self.MeetingNode(pHead)
if not meetingNode:
return None
NodeLoop = 1
flagNode = meetingNode
while flagNode.next != meetingNode:
NodeLoop += 1
flagNode = flagNode.next
pFast = pHead
for i in range(NodeLoop):
pFast = pFast.next
pSlow = pHead
while pFast != pSlow:
pFast = pFast.next
pSlow = pSlow.next
return pFast