-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path160.py
47 lines (44 loc) · 1.16 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
# 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 and headB):
return
A = headA
B = headB
countA = countB = 0
while A:
countA += 1
A = A.next
while B:
countB += 1
B = B.next
A = headA
B = headB
if countA > countB:
for i in range(countA - countB):
A = A.next
else:
for i in range(countB - countA):
B = B.next
while A:
if A == B:
return A
A = A.next
B = B.next
return
##########
class Solution(object):
def getIntersectionNode(self, headA, headB):
trav_a, trav_b = headA, headB
while trav_a is not trav_b:
trav_a = headB if not trav_a else trav_a.next
trav_b = headA if not trav_b else trav_b.next
return trav_a