-
Notifications
You must be signed in to change notification settings - Fork 0
/
52_get_common_node_of_linkedlist.py
47 lines (40 loc) · 1.13 KB
/
52_get_common_node_of_linkedlist.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
# -*- coding:utf-8 -*-
class ListNode:
def __init__(self, x):
self.val = x
self.next = None
class Solution:
def FindFirstCommonNode(self, pHead1, pHead2):
if pHead1 is None or pHead2 is None:
return None
loop1 = self.check_loop(pHead1)
loop2 = self.check_loop(pHead2)
# 都有环
if loop1 and loop2:
p = loop1
while p.next != loop1:
if p == loop2:
return loop1
p = p.next
return None
# 都无环
elif (not loop1) and (not loop2):
p1, p2 = pHead1, pHead2
while p1 != p2:
p1 = pHead1 if p1 is None else p1.next
p2 = pHead2 if p2 is None else p2.next
return p1
else:
return None
@staticmethod
def check_loop(pHead):
if pHead.next is None:
return False
buf_dict = {}
p = pHead
while p is not None:
if p in buf_dict:
return p
buf_dict[pHead] = 0
p = p.next
return False