[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 ...
随机推荐
- [BUGCASE]层叠上下文和z-index属性使用不当引发的文本被遮挡的问题
一.问题描述 在一个fixed-data-table(一个React组件)制作的表格中,需要给表头的字段一个提示的特效,所以做了一个提示层 这个提示层被固定(拖动表格的水平滚动条时固定)的表格列遮住 ...
- 在运行tsc编译.ts文件时,“因为在此系统上禁止运行脚本” 怎么解决?
tsc : 无法加载文件 C:\Users\Administrator\AppData\Roaming\npm\tsc.ps1,因为在此系统上禁止运行脚本.有关详细信息,请参阅 https:/go.m ...
- wirshark找不到本地接口
解决方法: 1.以管理员权限运行wireshark 2.以管理员身份运行cmd,输入net start npf,打开网络抓包服务,运行wireshark legacy,选择要抓包的网卡
- kali查看本机ip
- 微软最强 Python 自动化工具开源了!不用写一行代码!
1. 前言 最近,微软开源了一款非常强大的 Python 自动化依赖库:playwright-python 它支持主流的浏览器,包含:Chrome.Firefox.Safari.Microsoft E ...
- python多进程通讯踩坑记
# 错误代码如下 from multiprocessing import Process from queue import Queue # 正确代码应该是这样,Process和Queue都来自mul ...
- 小米ICPC第一场自闭记
这次终于找到了靠谱队友,比之前我做不出来==队友做不出来好太多了 昨天3人热身赛疯狂杀了8道题,感觉今天稳了 一开始就瞅了A题,发现似乎可以dp,看了看数据,1e7,大概想出了nsqrtn算法,想着肯 ...
- 2017 Mid Central Regional G.Hopscotch (组合计数)
这道题有点意思,给出点(N,N),你在原点处向目标点走,每次只能向x和y两个方向走路,每次xy两个方向的步幅分别不能小于dx和dy,问走到终点的方案数,答案对1e9 + 7取模 这道题最直接的想法就是 ...
- 第二十三章、 Model/View便利类表格部件QTableWidget详解
老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 一.引言 表格部件为应用程序提供标准的表格显示工具,在表格内可以管理基于行和列的数据项,表格中的最大 ...
- XSS挑战赛(2)
进入第六关 简单判断过滤情况 <>script"'/ 查看源代码 可以看到第二个红框部分跟之前类似,闭合双引号尝试进行弹窗 "><script>ale ...