-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIntersectionofTwoLinkedLists.py
37 lines (33 loc) · 1.06 KB
/
IntersectionofTwoLinkedLists.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
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
# get length of A
fakeA = headA
counta = 0
while fakeA != None:
counta += 1
fakeA = fakeA.next
# get length of B
fakeB = headB
countb = 0
while fakeB != None:
countb += 1
fakeB = fakeB.next
# make the two lists the same length
while counta != countb:
if counta > countb:
headA = headA.next
counta -= 1
else:
headB = headB.next
countb -= 1
# continue down the bigger list until the two lists are equal
while headA and headB:
if headA == headB:
return headA
if counta >= countb:
headA = headA.next
counta -= 1
else:
headB = headB.next
countb -= 1
# if the lists never equal each other, return None
return None