-
Notifications
You must be signed in to change notification settings - Fork 0
/
160.py
45 lines (39 loc) · 1010 Bytes
/
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
# 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
"""
if not headA or not headB:
return None
lenA = 0
lenB = 0
cur = headA
while cur:
lenA += 1
cur = cur.next
cur = headB
while cur:
lenB += 1
cur = cur.next
if lenA > lenB:
i = lenA - lenB
while i:
headA = headA.next
i -= 1
if lenB > lenA:
i = lenB - lenA
while i:
headB = headB.next
i -= 1
while headA or headB:
if headA == headB:
return headA
headA = headA.next
headB = headB.next
return None