-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path160-IntersectionOf2LL.py
37 lines (29 loc) · 1000 Bytes
/
160-IntersectionOf2LL.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
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = None
class Solution:
def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:
lengthA, lengthB = 1, 1
nodeA, nodeB = headA, headB
while(nodeA.next):
lengthA += 1
nodeA = nodeA.next
while(nodeB.next):
lengthB += 1
nodeB = nodeB.next
listAOffset, listBOffset = 0, 0
if(lengthA > lengthB): listAOffset = lengthA - lengthB
else: listBOffset = lengthB - lengthA
nodeA, nodeB = headA, headB
while(listAOffset > 0):
nodeA = nodeA.next
listAOffset -= 1
while(listBOffset > 0):
nodeB = nodeB.next
listBOffset -= 1
while(nodeA and nodeB):
if(nodeA is nodeB): return nodeA
nodeA = nodeA.next
nodeB = nodeB.next