def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
if not headA or not headB:
return None
pa = headA
pb = headB
while pa != pb:
pa = headB if pa is None else pa.next
pb = headA if pb is None else pb.next
# 如果两个链表没有交点,最终会同时到达尾部,此时 pa 和 pb 都为 None,返回 None
if pa == None and pb == None:
return None
return pa # 返回交点或者 None
难点:理解两个指针遍历链表,反复重定向,若有交点则总会相遇这一过程。
下面这位描述地很好: