160. 相交链表

编写一个程序,找到两个单链表相交的起始节点。

如下面的两个链表

输入:intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
输出:Reference of the node with value = 8
输入解释:相交节点的值为 8 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [4,1,8,4,5],链表 B 为 [5,0,1,8,4,5]。在 A 中,相交节点前有 2 个节点;在 B 中,相交节点前有 3 个节点。

输入:intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
输出:Reference of the node with value = 2
输入解释:相交节点的值为 2 (注意,如果两个列表相交则不能为 0)。从各自的表头开始算起,链表 A 为 [0,9,1,2,4],链表 B 为 [3,2,4]。在 A 中,相交节点前有 3 个节点;在 B 中,相交节点前有 1 个节点。
输入:intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
输出:null
输入解释:从各自的表头开始算起,链表 A 为 [2,6,4],链表 B 为 [1,5]。由于这两个链表不相交,所以 intersectVal 必须为 0,而 skipA 和 skipB 可以是任意值。
解释:这两个链表不相交,因此返回 null。

注意:

  • 如果两个链表没有交点,返回 null.
  • 在返回结果后,两个链表仍须保持原有的结构。
  • 可假定整个链表结构中没有循环。
  • 程序尽量满足 O(n) 时间复杂度,且仅用 O(1) 内存。
# 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
        """
        save = set()
        curA, curB = headA, headB
        while curA is not None or curB is not None:
            if curA is not None:
                if curA in save:
                    return curA
                else:
                    save.add(curA)
                    curA = curA.next
            if curB is not None:
                if curB in save:
                    return curB
                else:
                    save.add(curB)
                    curB = curB.next
        return None
    # 利用双指针,也可以利用快慢指针
    def getIntersectionNode2(self, headA, headB):
        """
        :type head1, head1: ListNode
        :rtype: ListNode
        """
        # 定义两个指针, 第一轮让两个到达末尾的节点指向另一个链表的头部, 最后如果相遇则为交点(在第一轮移动中恰好抹除了长度差)
        # 两个指针等于移动了相同的距离, 有交点就返回, 无交点就是各走了两条指针的长度
        curA, curB = headA, headB
        # 在这里第一轮体现在pA和pB第一次到达尾部会移向另一链表的表头, 而第二轮体现在如果pA或pB相交就返回交点,
        # 不相交最后就是null == null
        while curA != curB:
            if curA is None:
                curA = headB
            else:
                curA = curA.next
            if curB is None:
                curB = headA
            else:
                curB = curB.next
        return curA

    # 法3: 求出两个链表A和B的长度, 长链表先走|len(A)-len(B)|步, 然后同时遍历返回第一个公共节点.
    def getIntersectionNode3(self, headA, headB):

        a = headA
        b = headB
        a_num = 0
        b_num = 0
        while a:
            a = a.next
            a_num += 1
        while b:
            b = b.next
            b_num += 1

        if a_num >= b_num:
            gap = a_num - b_num
            while gap:
                headA = headA.next
                gap -= 1
            while headA and headB:
                if headA == headB:
                    return headA
                headA = headA.next
                headB = headB.next
            return
        else:
            gap = b_num - a_num
            while gap:
                headB = headB.next
                gap -= 1
            while headA and headB:
                if headA == headB:
                    return headA
                headA = headA.next
                headB = headB.next
            return

if __name__ == '__main__':
    s = Solution()
    l1 = ListNode(0)
    l1.next = ListNode(9)
    l1.next.next = ListNode(1)
    l1.next.next.next = ListNode(2)
    l1.next.next.next.next = ListNode(4)

    l2 = ListNode(3)
    l2.next = l1.next.next.next
    print(s.getIntersectionNode3(l1, l2).val)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值