





python
# 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]:
if not headA or not headB:
return None
pA, pB = headA, headB
while pA != pB:
# 当 pA 走完 A 链表后,跳到 B 链表
pA = pA.next if pA else headB
# 当 pB 走完 B 链表后,跳到 A 链表
pB = pB.next if pB else headA
return pA # 若相交返回交点,否则返回 None