相交链表
https://leetcode.cn/problems/intersection-of-two-linked-lists/ (opens new window) 需要注意的点 1.链表a和b组合起来,如果有交点,
# 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:
if not headA or not headB:
return None
pa = headA
pb = headB
while pa != pb:
if pa:
pa = pa.next
else:
pa = headB
if pb:
pb = pb.next
else:
pb = headA
return pa