leetcode 160. Intersection of Two Linked Lists 常数内存 python3

一.问题描述

Write a program to find the node at which the intersection of two singly linked lists begins.

For example, the following two linked lists:

begin to intersect at node c1.

Example 1:

Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3
Output: Reference of the node with value = 8
Input Explanation: The intersected node's value is 8 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [4,1,8,4,5]. From the head of B, it reads as [5,0,1,8,4,5]. There are 2 nodes before the intersected node in A; There are 3 nodes before the intersected node in B.

Example 2:

Input: intersectVal = 2, listA = [0,9,1,2,4], listB = [3,2,4], skipA = 3, skipB = 1
Output: Reference of the node with value = 2
Input Explanation: The intersected node's value is 2 (note that this must not be 0 if the two lists intersect). From the head of A, it reads as [0,9,1,2,4]. From the head of B, it reads as [3,2,4]. There are 3 nodes before the intersected node in A; There are 1 node before the intersected node in B.

Example 3:

Input: intersectVal = 0, listA = [2,6,4], listB = [1,5], skipA = 3, skipB = 2
Output: null
Input Explanation: From the head of A, it reads as [2,6,4]. From the head of B, it reads as [1,5]. Since the two lists do not intersect, intersectVal must be 0, while skipA and skipB can be arbitrary values.
Explanation: The two lists do not intersect, so return null.

Notes:

  • If the two linked lists have no intersection at all, return null.
  • The linked lists must retain their original structure after the function returns.
  • You may assume there are no cycles anywhere in the entire linked structure.
  • Your code should preferably run in O(n) time and use only O(1) memory.

二.解题思路

这道题O(N)的时间要求很简单,如果没有O(1)的内存要求的话,

我们只需要将其中一个链表的每个节点存进set,然后遍历另外一个链表,如果遍历的节点在集合中,就说明交叉点是这个,返回。

难点就是常数内存的要求。

观察可知,如果两链表长度相同,并且存在这样的交叉点,那么我们只需要同时迭代两链表,并比较当前两节点是否相同,相同则返回,不同则继续下次迭代。

问题就是如果两链表长度不相同怎么办?

我们可以让他们相同(对齐)。如果我们知道了一个链表比另外一个链表长 k,那么我们先让长的那个链表迭代k次(对齐了),之后再同时迭代两链表,用上述方法得到结果。

综上所述,我们需要做的是计算两链表的长度差值,然后对齐两链表,之后迭代。

关于计算链表差值,可以一次迭代计算两链表的长度差值,也可以分别计算两链表的长度然后再相减。

时间复杂度O(N),空间复杂度O(1)

更多leetcode算法题解法: 专栏 leetcode算法从零到结束  或者 leetcode 解题目录 Python3 一步一步持续更新~

三.源码

class Solution:
    def getIntersectionNode(self, headA: ListNode, headB: ListNode) -> ListNode:
        difference,head1,head2,res=0,headA,headB,None
        while headA and headB:
            if headA==headB:return headA
            headA,headB=headA.next,headB.next
        if not headA and not headB:return res
        if headA: # temp and temp1: the longer linked list, temp2, the shorter one
            temp=headA
            temp1,temp2=head1,head2
        else:
            temp=headB
            temp1,temp2=head2,head1
        while temp: # Aligned linked list 
            temp=temp.next
            temp1=temp1.next
        while temp1 and temp2:
            if temp1==temp2:
                res=temp1
                break
            temp1,temp2=temp1.next,temp2.next
        return res
发布了218 篇原创文章 · 获赞 191 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/CSerwangjun/article/details/103467945