forked from nidhisha-shetty/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intersection-of-two-linkedlists.py
44 lines (38 loc) · 1.15 KB
/
intersection-of-two-linkedlists.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
'''
P.S: Given the heads of two singly linked-lists headA and headB, return the node at which the two lists intersect.
If the two linked lists have no intersection at all, return null.
'''
#Solution:
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
countA=0
countB=0
temp_headA=headA
temp_headB=headB
while temp_headA:
temp_headA=temp_headA.next
countA+=1
while temp_headB:
temp_headB=temp_headB.next
countB+=1
if countA>countB:
for _ in range(countA-countB):
headA=headA.next
if countB>countA:
for _ in range(countB-countA):
headB=headB.next
while headA and headB:
if headA == headB:
return headA
else:
headA=headA.next
headB=headB.next
return None
else:
headA=headA.next
headB=headB.next
return None