-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path160.py
66 lines (58 loc) · 1.72 KB
/
160.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
63
64
65
66
'''
160. Intersection of Two Linked Lists
https://leetcode.com/problems/intersection-of-two-linked-lists/
Hi, here's your problem today. This problem was recently asked by Apple:
You are given two singly linked lists. The lists intersect at some node.
Find, and return the node. Note: the lists are non-cyclical.
Example:
A = 1 -> 2 -> 3 -> 4
B = 6 -> 3 -> 4
This should return 3 (you may assume that any nodes with the same value are the same node).
'''
class Solution(object):
def getIntersectionNode(self, headA, headB):
nodeA = headA
nodeB = headB
AtoB = BtoA = False
while nodeA and nodeB and nodeA != nodeB:
nodeA = nodeA.next
if nodeA is None:
if AtoB:
return None
AtoB = True
nodeA = headB
nodeB = nodeB.next
if nodeB is None:
if BtoA:
return None
BtoA = True
nodeB = headA
return nodeA if nodeA == nodeB else None
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
def __str__(self):
s = ''
c = self
while c:
s += str(c.val) + ' '
c = c.next
return s
rootA = ListNode(1)
rootA.next = ListNode(2)
rootA.next.next = ListNode(3)
rootA.next.next.next = ListNode(4)
rootB = ListNode(6)
rootB.next = rootA.next.next
print(Solution().getIntersectionNode(rootA, rootB))
# 3 4
rootA = ListNode(1)
rootA.next = ListNode(2)
rootA.next.next = ListNode(3)
rootA.next.next.next = ListNode(4)
rootB = ListNode(6)
rootB.next = ListNode(7)
rootB.next.next = ListNode(8)
print(Solution().getIntersectionNode(rootA, rootB))
# None