Intersection of Two Linked Lists (LC-0160)
class Solution:
def getIntersectionNode(self, headA, headB):
L1, L2 = 0, 0
temp = headA
while temp: temp, L1 = temp.next, L1 + 1
temp = headB
while temp: temp, L2 = temp.next, L2 + 1
if L1 <= L2:
l2 = headB
for ix in range(L2 - L1):
l2 = l2.next
l1 = headA
while (l1 and l2) and (l1 != l2):
l1, l2 = l1.next, l2.next
return l1 if l1 else None
l1 = headA
for ix in range(L1 - L2):
l1 = l1.next
l2 = headB
while (l1 and l2) and (l1 != l2):
l1, l2 = l1.next, l2.next
return l1 if l1 else None