# Definition for singly-linked list. # class ListNode: # def __init__(self, x): # self.val = x # self.next = None
classSolution: defgetIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode: a = 0 b = 0 p = headA q = headB # 先计算各自长度 while p: p = p.next a += 1
while q: q = q.next b += 1 p = headA q = headB print('a,b', a, b) c = a - b if c > 0: p = headA q = headB else: p = headB q = headA c = -c
# 长链表先走,然后一起走 while c: ifnot p ornot q: returnNone p = p.next c = c - 1 while p and q: if p == q: return p p = p.next q = q.next returnNone