160. 相交链表 - 力扣(LeetCode)
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, x):
# self.val = x
# self.next = Noneclass Solution:def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> Optional[ListNode]:if not headA or not headB:return NonepA, pB = headA, headBwhile pA != pB:# 当 pA 走完 A 链表后,跳到 B 链表pA = pA.next if pA else headB# 当 pB 走完 B 链表后,跳到 A 链表pB = pB.next if pB else headAreturn pA # 若相交返回交点,否则返回 None