LeetCode 160. Intersection of Two Linked Lists

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

v2-eb1a908f54848e8f78d1b6764e2b994a_1440w.jpg

Description



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


For example, the following two linked lists:


v2-c361b7bd13848a966981a881de649e2e_720w.jpg


begin to intersect at node c1.


Example 1:


v2-ef72798e0bd07fd56bd34c4d48a37154_720w.jpg


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:


v2-b306c86501ca2a76d258c3c2583e7921_720w.jpg


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:


v2-b9c0ae49045dcab7c1de43eb3c09daeb_720w.jpg


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.


描述



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


思路



  • 这道题有两种不同的写法,其基本思路是一致的.
  • 在上面的示例中,相交的节点我们称之为c,如果c点前面A,B链表的节点个数是相同的,那么我们从A,B的起点同时往后走,就一定会相遇.
  • 于是我们就要想办法把相交的节点前面节点数目变得一样.
  • 方法一,我们分别获取链表A和链表B的长度lena和lenb,我们让其中较长的链表先走lena-lanb步(A比B长,如果B更长就是lenb-lena步),然后让两个链表同时往后面走.


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-01-16 09:55:59
# @Last Modified by:   何睿
# @Last Modified time: 2019-01-16 10:10:29
# 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 not headA or not headB: return None
        # 获取链表的长度
        lena, lenb = self._lenList(headA), self._lenList(headB)
        a, b = headA, headB
        # 移动起始位置到节点个数相同的第一个点
        if lena > lenb:
            for _ in range(lena - lenb):
                a = a.next
        elif lenb > lena:
            for _ in range(lenb - lena):
                b = b.next
        # 同时往后面走
        while a != b:
            a, b = a.next, b.next
        return a
    # 辅助函数,获取链表的长度
    def _lenList(self, Node):
        res = 0
        while Node:
            res += 1
            Node = Node.next
        return res


  • 方法二,为了获得相同个数的链表个数的效果,我们可以把链表A添加到链表B上,把链表B添加到链表B上.
  • 体现的形式是先从链表A出发的节点如果走完了链表A,就接着链表B开始走,反过来先从链表B出发的链表也一样.


# -*- coding: utf-8 -*-
# @Author:             何睿
# @Create Date:        2019-01-16 09:55:59
# @Last Modified by:   何睿
# @Last Modified time: 2019-01-16 10:10:29
# 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 not headA or not headB:
            return None
        a, b = headA, headB
        # 另一种写法,为了达到从相同个数启起点,我们把a链表添加到b链表前面
        # 把b链表添加到a链表前面
        while a != b:
            if not a:
                a = headB
            elif not b:
                b = headA
            else:
                a, b = a.next, b.next
        return a


源代码文件在这里.


目录
相关文章
|
存储 缓存 算法
LeetCode刷题---Two Sum(一)
LeetCode刷题---Two Sum(一)
Leetcode 4. Median of Two Sorted Arrays
题目描述很简单,就是找到两个有序数组合并后的中位数,要求时间复杂度O(log (m+n))。 如果不要去时间复杂度,很容易就想到了归并排序,归并排序的时间复杂度是O(m+n),空间复杂度也是O(m+n),不满足题目要求,其实我开始也不知道怎么做,后来看了别人的博客才知道有个二分法求两个有序数组中第k大数的方法。
39 0
|
存储 C++ Python
LeetCode刷题---Add Two Numbers(一)
LeetCode刷题---Add Two Numbers(一)
|
存储 算法 安全
LeetCode - #2 Add Two Numbers
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #2 Add Two Numbers
|
存储 算法 安全
LeetCode - #1 Two Sum
我们社区从本期开始会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。)的 Swift 算法题题解整理为文字版以方便大家学习与阅读。 不积跬步,无以至千里;不积小流,无以成江海,Swift社区 伴你前行。
LeetCode - #1 Two Sum
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 141. 环形链表 Linked List Cycle
LeetCode 237. 删除链表中的节点 Delete Node in a Linked List
LeetCode 237. 删除链表中的节点 Delete Node in a Linked List
LeetCode Contest 178-1367. 二叉树中的列表 Linked List in Binary Tree
LeetCode Contest 178-1367. 二叉树中的列表 Linked List in Binary Tree
LeetCode 167 Two Sum II - Input array is sorted(输入已排序数组,求其中两个数的和等于给定的数)
给定一个有序数组和一个目标值 找出数组中两个成员,两者之和为目标值,并顺序输出
86 0
LeetCode 234. 回文链表 Palindrome Linked List
LeetCode 234. 回文链表 Palindrome Linked List