[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 ...
随机推荐
- C语言位运算符详解
原文链接:https://www.cnblogs.com/911/archive/2008/05/20/1203477.html 位运算是指按二进制进行的运算.在系统软件中,常常需要处理二进制位的问题 ...
- Unity全局调用非静态函数
Unity全局调用非静态函数 情形 大概就是做游戏的时候想做一个给玩家展示信息的东西,比如玩家按了不该按的键提醒一下之类的.这个脚本倒是很简单,找个Text组件往上面加字就行了.问题在于这个脚本游戏中 ...
- 【2014广州市选day1】JZOJ2020年9月12日提高B组T3 消除游戏
[2014广州市选day1]JZOJ2020年9月12日提高B组T3 消除游戏 题目 Description 相信大家玩过很多网络上的消除类型的游戏,一般来说就是在一个大拼图内找出相同的部分进行最大程 ...
- Centos7 之间的文件拷贝
环境: 内网了两台cenots7主机 scp命令 scp [参数] [原路径] [目标路径] scp -P 22022 /home/file.war root@192.168.253.172:/hom ...
- 第3.3节 强大的Python列表
一. 列表切片操作补充 列表切片支持所有序列切片的方法,以倒序切片和步长大于1的情况再举例验证一下: l=[1,2,3,4,5] l[::2] #结果[1, 3, 5] l[-1::2] #结果[5] ...
- 第15.47节、PyQt显示部件:QGraphicsView图形视图和QGraphicsScene图形场景简介及应用案例
专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.概述 Designer中的Graphics V ...
- 第8.33节 Python中__getattr__以及__getattr__与__ getattribute__的关系深入剖析
一. 引言 前面几节分别介绍了Python中属性操作捕获的三剑客:__ getattribute__方法.__setattr__方法.__delattr__方法,为什么__ getattribute_ ...
- PyQt(Python+Qt)学习随笔:model/view架构中类QStandardItemModel的使用方法
老猿Python博文目录 老猿Python博客地址 一.概述 QStandardItemModel是QAbstractItemModel的派生类,用于在Model/View架构中存储自定义数据的通用模 ...
- VMware 12 与Centos 7建立共享文件夹 && vmware 15 pro过期激活秘钥
1 vmware 15pro评估过期 网上找到的激活秘钥,激活密钥: YG5H2-ANZ0H-M8ERY-TXZZZ-YKRV8(亲试有效)UG5J2-0ME12-M89WY-NPWXX-WQH88U ...
- iOS崩溃日志 如何看
日志主要分为六个部分:进程信息.基本信息.异常信息.线程回溯.线程状态和二进制映像. 我们在进行iPhone应用测试时必然会在"隐私"中找到不少应用的崩溃日志,但是不会阅读对于很多 ...