-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy path160.Intersection-of-Two-Linked-Lists.py
50 lines (41 loc) · 1.13 KB
/
160.Intersection-of-Two-Linked-Lists.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
# https://leetcode.com/problems/intersection-of-two-linked-lists/
#
# algorithms
# Easy (31.19%)
# Total Accepted: 254,398
# Total Submissions: 815,597
# 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
"""
tmp_a, tmp_b = headA, headB
len_a, len_b = 0, 0
while tmp_a:
len_a += 1
tmp_a = tmp_a.next
while tmp_b:
len_b += 1
tmp_b = tmp_b.next
if len_a == 0 or len_b == 0:
return None
if len_a > len_b:
while len_a > len_b:
headA = headA.next
len_a -= 1
elif len_b > len_a:
while len_b > len_a:
headB = headB.next
len_b -= 1
while headA and headB:
if headA == headB:
return headA
headA = headA.next
headB = headB.next
return None