LeetCode 160: 相交链表 Intersection of Two Linked Lists
爱写Bug(ID:iCodeBugs)
编写一个程序,找到两个单链表相交的起始节点。
Write a program to find the node at which the intersection of two singly linked lists begins.
如下面的两个链表:

在节点 c1 开始相交。
示例 1:

输入: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 个节点。
示例 2:**

输入: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 个节点。
示例 3:**

输入: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) 内存。 相交链表
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) 空间复杂度解题,最容易想到的就是:遍历链表得到链表长度,计算双链表差值,长链表减去差值得到的剩下的链表与短链表长度相等,然后开始比较地址。,当遇到第一个节点地址相同时即为所求。
这个原理简单,但是代码太冗长了,我们来看另一种更优雅的解法,看图帮助理解:

定义两个指针curA、curB分别从链表A、B开始遍历链表,此时最先遇到空节点(到达末尾)的链表即为短链表。
curA、curB走过的长度均为LenA。
此时将curA指向链表 B (长链表)的头节点,curB 不变。两个指针开始第二段遍历,直到 curB 遇到空节点(长链表到达末尾)。
此时 curB 走完整个长链表,走的第二段长度就是差值 L。CurA 从长链表头节点开始,与 curB 同步,其走的第二段长度同样是差值 L。
此时 curA 在长链表 剩余未遍历的节点长度是:LenB - L = LenA
此时将 curB 指向链表A的头节点,即将遍历链表A 的长度为 LenA ,则curA、curB剩余即将遍历的长度即为原本需要比较的长度。
开始第三段遍历,遇到第一个相同节点时返回即可。
代码:
Java:
public class Solution {
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA == null || headB == null) {
return null;
}
ListNode curA = headA;
ListNode curB = headB;
while (curA != curB) {
curA = curA == null ? headB : curA.next;
curB = curB == null ? headA : curB.next;
}
return curA;
}
}
Python:
class Solution(object):
def getIntersectionNode(self, headA, headB):
"""
:type head1, head1: ListNode
:rtype: ListNode
"""
if not headA or not headB:
return None
curA , curB = headA , headB
while curA != curB:
curA = curA.next if curA else headB
curB = curB.next if curB else headA
return curA
欢迎关注公众号一起学习:爱写Bug

LeetCode 160: 相交链表 Intersection of Two Linked Lists的更多相关文章
- [Swift]LeetCode160. 相交链表 | Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Java实现 LeetCode 160 相交链表
160. 相交链表 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4, ...
- LeetCode 160——相交链表(JAVA)
编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], listB ...
- LeetCode 160. 相交链表(Intersection of Two Linked Lists)
题目描述 编写一个程序,找到两个单链表相交的起始节点. 例如,下面的两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始相交. 注意: ...
- LeetCode 160 相交链表
题目: 编写一个程序,找到两个单链表相交的起始节点. 如下面的两个链表: 在节点 c1 开始相交. 示例 1: 输入:intersectVal = 8, listA = [4,1,8,4,5], li ...
- leetcode 160相交链表
暴力解法当然可以遍历两个链表,不过time O(mn) space O(1)暂且不说, 方法一:双指针, time O(m+n),space O(1) 可以对比判断环形链表的快慢指针法. 这种方法构思 ...
- LeetCode 160. 相交链表 (找出两个链表的公共结点)
题目链接:https://leetcode-cn.com/problems/intersection-of-two-linked-lists/ 编写一个程序,找到两个单链表相交的起始节点. 如下面的两 ...
- [LeetCode]160.Intersection of Two Linked Lists(2个链表的公共节点)
Intersection of Two Linked Lists Write a program to find the node at which the intersection of two s ...
- [LeetCode] 160. Intersection of Two Linked Lists 解题思路
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
随机推荐
- selenium三大切换的骚操作之显性等待
一.handle窗口切换 当点击某个元素后,会重新生成一个新的页签,但此时我们的操作仍然在原先的窗口当中,如果要在新的窗口继续操作元素,那么就要用到handle窗口切换的方法. 常用方法: windo ...
- MongoDB自学------(5)MongoDB分片
分片 在Mongodb里面存在另一种集群,就是分片技术,可以满足MongoDB数据量大量增长的需求. 当MongoDB存储海量的数据时,一台机器可能不足以存储数据,也可能不足以提供可接受的读写吞吐量. ...
- VSCode中代码在浏览器中打开及实时刷新
实时刷新方法一: 在项目目录下运行命令: browser-sync start --server --files "**/*.css,**/*.html,**/*.js" 实施刷新 ...
- python爬虫:将数据保存到本地
一.python语句存储 1.with open()语句 with open(name,mode,encoding) as file: file.write() name:包含文件名称的字符串; mo ...
- SPA项目搭建及嵌套路由
Vue-cli: 什么是vue-cli? vue-cli是vue.js的脚手架,用于自动生成vue.js+webpack的项目模板,创建命令如下: vue init webpack xxx 注1:xx ...
- Javase之集合体系(2)之List及其子类ArrayList,LinkedList与Vector及其迭代器知识
集合体系之List及其子类ArrayList,LinkedList与Vector及其迭代器知识 List(接口) 特点:有序(存储与取出顺序相同),可重复 List子类特点: ArrayList: ...
- C# 结构与类
结构是一种可以包含数据成员和方法成员的值类型数据结构.为结构分配数据时不需要从托管堆中分配内存,结构类型的变量直接包含了该结构的数据.结构中可以包含构造函数,常量,字段方法,属性,运算符,事件和嵌套类 ...
- FCC---Make Motion More Natural Using a Bezier Curve--- juggling movement
This challenge animates an element to replicate the movement of a ball being juggled. Prior challeng ...
- 【Web】解决简书图片不显示问题“系统维护中,图片暂时无法加载”
简书不显示图片的解决方法 首次编辑于2019-6-6 最近几天在浏览简书上的文章时,发现图片显示不出来,提示"系统维护中,图片暂时无法加载". 猜测应该是简书由于某种原因暂时屏蔽了 ...
- zip unzip 压缩解压缩命令
直接上例子: mkdir test1 touch test1/1.txt touch test1/2.txt zip -r test1.zip test1 #-r 参数是包含文件夹下的文件 un ...