[LeetCode]求两个链表的焦点--Intersection of Two Linked Lists
1.解题意
求解两个链表的焦点,这个交点并不是焦点的值相等,而是需要交点之后的数据是完全相等的。
落实到java层面,就是交点处的对象是同一个对象即可。
ps:我最开始没有读懂题目,然后就开始比较节点的值是否相等,直到示例跑失败了才知道原因。
2.通用解法
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if(headA!=null && headB!=null){
ListNode tempA=headA;
ListNode tempB=headB;
while(tempA!=null){
while(tempB!=null){
if(tempA==tempB){
return tempA;
}
tempB=tempB.next;
}
tempB=headB;
tempA=tempA.next;
}
}
return null;
}
这里通过双循环的方式来遍历每一个节点,如果节点是同一个对象即可返回,这是我们正常的思维。
接下来的结果就打脸了:时间复杂度|空间复杂度 超过5%的解法。
哎,还是读题的问题,题目要求了时间复杂度是 O(n),空间复杂度是O(1);那么应该是有更优解的。
3.更优解
首先,假定存在交点,交点之后的链表长度是 c,链表A在交点之前的长度是a,链表B在交点之前的长度是b;
A的长度是 a+c;B的长度是 b+c
如果我们让两边相等,即把长度变成是 a+b+c,那么链表A的最后一个节点是链表B交点之前的节点,即下一个节点就是交点;同理链表B的最后一个节点的下一个节点就是交点;
这样,我们可以只遍历一次,就可以找到交点,最大的长度是 a+b+c+1,即可找到交点;
代码如下:
public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
ListNode l1 = headA, l2 = headB;
int finishA = 0;
boolean finishB = false;
while (l1 != l2 && finishA<3) {
if(l1==null){
finishA++;
}
if(l2==null){
finishA++;
}
l1 = (l1 == null) ? headB : l1.next;
l2 = (l2 == null) ? headA : l2.next;
}
return finishA>=3?null:l1; // 修改当无交点时的死循环
}
参考:https://cyc2018.github.io/CS-Notes/#/notes/Leetcode 题解 - 链表
[LeetCode]求两个链表的焦点--Intersection of Two Linked Lists的更多相关文章
- 两个链表的交叉 · Intersection of Two Linked Lists
[抄题]: Write a program to find the node at which the intersection of two singly linked lists begins. ...
- [LeetCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- [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 ...
- [LintCode] Intersection of Two Linked Lists 求两个链表的交点
Write a program to find the node at which the intersection of two singly linked lists begins. Notice ...
- LeetCode之“链表”:Intersection of Two Linked Lists
此题扩展:链表有环,如何判断相交? 参考资料: 编程判断两个链表是否相交 面试精选:链表问题集锦 题目链接 题目要求: Write a program to find the node at whic ...
- intersection of two linked lists.(两个链表交叉的地方)
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉
题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...
- [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判断交叉链表的交点
方法要记住,和判断是不是交叉链表不一样 方法是将两条链表的路径合并,两个指针分别从a和b走不同路线会在交点处相遇 public ListNode getIntersectionNode(ListNod ...
随机推荐
- spring与分布式事务
转载:https://www.cnblogs.com/qianjun2017/p/8349829.html 转载:https://blog.csdn.net/jaryle/article/detail ...
- Android中Application的意义及使用方法
首先,在一个Android程序中,有且只有一个Application对象,在程序启动的时候,首先执行Application的onCreate方法,这是一个Android应用的入口,在开发中,我们常常自 ...
- 洛谷 P3410 拍照(最大流 + 建图)
这道题问的是一群人要和另一群人合影,每个客人都有必须在场的人全部在场才能在场,每个客人给的有收入,但是邀请也需要支出,问最大收入? 我觉得可以总结为一类问题,就是有先决条件的网络流问题.看到费用和支出 ...
- JDK8日期类入门
关于jdk8的时间类的用法,网上有很多教程教你如何用,比如: System.out.println(LocalDateTime.now()); 可以获取当前的时间, 2020-12-06T18:02: ...
- moviepy音视频开发:音频合成类AudioArrayClip介绍
☞ ░ 前往老猿Python博文目录 ░ AudioArrayClip类是AudioClip的直接子类,用于从一个numpy音频数组构建音频剪辑.AudioArrayClip类只有一个构造方法,在构造 ...
- PyQt学习随笔:Qt事件QEvent.type类型常量及其含义资料汇总详细内容速查
下表是Qt5.11提供的所有已经定义的事件类型常量及其含义说明(其中标蓝色的是老猿认为价值比较大的事件),事件的事件类型通过QEvent.type()来获取.由于老猿没有找到直接粘贴Excel表格的方 ...
- CNVD漏洞证书(1)
之前申请了CNVD原创漏洞,踩了坑,记录一下 有很多师傅写过相关的文章: https://blog.csdn.net/qq1124794084/article/details/82657840 htt ...
- git——同步本地文件到github上
参考教程: 1.https://blog.csdn.net/weixin_37769855/article/details/99439904 2.https://www.liaoxuefeng.com ...
- 利用IDEA把Java项目打成jar包
第一步:按如下步骤或Ctrl+Shift+Alt+S打开 Project Structure第二步:第三步:选择要执行的文件, 依次选择项目, main方法所在的文件, 保存如果出现以下错误:则根据 ...
- Pytorch训练时显存分配过程探究
对于显存不充足的炼丹研究者来说,弄清楚Pytorch显存的分配机制是很有必要的.下面直接通过实验来推出Pytorch显存的分配过程. 实验实验代码如下: import torch from torch ...