相交链表

2023-04-16  本文已影响0人  HellyCla
image.png
# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution(object):
    def getIntersectionNode(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        if headA is None or headB is None:
            return None
        pA = headA
        pB = headB
        while pA != pB and (pA or pB):
            if pA is None:
                pA=headB
            else:
                pA=pA.next
            if pB is None:
                pB=headA
            else:
                pB=pB.next
        if pA is None:
            return None
        else:
            return pA
         ```
上一篇下一篇

猜你喜欢

热点阅读