-
Notifications
You must be signed in to change notification settings - Fork 0
/
getIntersectionNode.py
70 lines (58 loc) · 1.47 KB
/
getIntersectionNode.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
67
68
69
70
# coding:utf-8
# https://leetcode.com/problems/intersection-of-two-linked-lists/ (160. Intersection of Two Linked Lists)
# Definition for singly-linked list.
class ListNode(object):
def __init__(self, x):
self.val = x
self.next = None
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
a, b = 0, 0
p = headA
q = headB
while p:
a += 1
p = p.next
while q:
b += 1
q = q.next
p =headA
q =headB
if a > b:
dif = a - b
while dif > 0:
p = p.next
dif -= 1
elif b > a:
dif = b - a
while dif > 0:
q = q.next
dif -= 1
while p:
if p == q:
return p
p = p.next
q = q.next
return None
if __name__ == '__main__':
head1 = ListNode('a1')
head2 = ListNode('b1')
node1 = ListNode('a2')
node2 = ListNode('b2')
node3 = ListNode('b3')
node4 = ListNode('c1')
node5 = ListNode('c2')
node6 = ListNode('c3')
head1.next = node1
node1.next = node4
node4.next = node5
node5.next = node6
head2.next = node2
node2.next = node3
node3.next = node4
exe = Solution()
print exe.getIntersectionNode(head1, head2).val