160.Intersection of Two Linked Lists
# 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) -> ListNode: lenA, lenB = 0, 0 pA, pB = headA, headB while pA: lenA += 1 pA = pA.next while pB: lenB += 1 pB = pB.next if lenA > lenB: for _ in range(lenA - lenB): headA = headA.next else: for _ in range(lenB - lenA): headB = headB.next while headA: if headA == headB: return headA else: headA = headA.next headB = headB.next return None