-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
链表中环的入口结点.py
62 lines (54 loc) · 1.37 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
'''
一个链表中包含环,请找出该链表的环的入口结点。
'''
# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def MeetingNode(self, pHead):
if pHead == None:
return None
pSlow = pHead.next
if pSlow == None:
return None
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
node1 = ListNode(1)
node2 = ListNode(2)
node3 = ListNode(3)
node4 = ListNode(4)
node5 = ListNode(5)
node6 = ListNode(6)
node1.next = node2
node2.next = node3
node3.next = node4
node4.next = node5
node5.next = node6
node6.next = node3
s = Solution()
print(s.EntryNodeOfLoop(node1).val)