标题题目地址

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的更多相关文章

  1. 两个链表的交叉 · Intersection of Two Linked Lists

    [抄题]: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  2. [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 ...

  3. [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 ...

  4. [LintCode] Intersection of Two Linked Lists 求两个链表的交点

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

  5. LeetCode之“链表”:Intersection of Two Linked Lists

    此题扩展:链表有环,如何判断相交? 参考资料: 编程判断两个链表是否相交 面试精选:链表问题集锦 题目链接 题目要求: Write a program to find the node at whic ...

  6. intersection of two linked lists.(两个链表交叉的地方)

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

  7. lintcode 中等题:Intersection of Two Linked Lists 两个链表的交叉

    题目 两个链表的交叉 请写一个程序,找到两个单链表最开始的交叉节点. 样例 下列两个链表: A: a1 → a2 ↘ c1 → c2 → c3 ↗ B: b1 → b2 → b3 在节点 c1 开始交 ...

  8. [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 ...

  9. [LeetCode]160. Intersection of Two Linked Lists判断交叉链表的交点

    方法要记住,和判断是不是交叉链表不一样 方法是将两条链表的路径合并,两个指针分别从a和b走不同路线会在交点处相遇 public ListNode getIntersectionNode(ListNod ...

随机推荐

  1. 集群--lvs

    快一个月没有更新博客了,最近一段时间在忙世界技能大赛网络系统系管理这个项目,没有太多的时间,我百忙之中更新一下.最近赛题中有说到集群这个,lvs这个东西(我也该学学这个了,一直停留在基础部分,是时候学 ...

  2. 获取Win和Linux系统启动时间,类似uptime功能,用于判断是否修改过系统时间

    目录 前言 测试代码 Win测试 Linux测试 总结 前言 有时候需要判断系统是否有修改过时间,最简单的方法就是获取当前时间A,然后sleep X秒,然后获取 时间B,如果 时间B - 时间A ≠ ...

  3. 跟随杠精的视角一起来了解Redis的主从复制

    不想弹好吉他的撸铁狗,都不是好的程序猿 虽然说单机的Redis性能很好,也有完备的持久化机制,那如果你的业务体量真的很大,超过了单机能够承载的上限了怎么办?不做任何处理的话Redis挂了怎么办?带着这 ...

  4. 第4.4节 Python解析与推导:列表解析、字典解析、集合解析

    一.    引言 经过前几个章节的介绍,终于把与列表解析的前置内容介绍完了,本节老猿将列表解析.字典解析.集合解析进行统一的介绍. 前面章节老猿好几次说到了要介绍列表解析,但老猿认为涉及知识层面比较多 ...

  5. PyQt学习随笔:ListView控件获取当前选择项的方法

    通过currentIndex()可以获取listView控件的当前选择元素,如果选择了多个,则可以通过selectedIndexes()来获取选择的元素,不过这两个函数返回的是元素数据,而不是索引编号 ...

  6. JAVA课堂作业(2019.10.14)

    一. (1)代码 package class20191014; import java.util.Scanner; public class ClassHomework { public static ...

  7. day010|python之装饰器

    装饰器02 目录 装饰器02 1 装饰器的语法糖 1.1 定义 1.2 基本使用 2 有参装饰器 2.1 基本用法 2.2 示例 3叠加多个装饰器 3.1 基本用法 3.2 示例 4 wraps装饰器 ...

  8. [BJDCTF 2nd]old-hack && [GXYCTF2019]禁止套娃

    [BJDCTF 2nd]old-hack 页面很有意思 同时也告诉了我们是THINKPHP5,我们只需要寻找THINKPHP5的漏洞就可以了. https://www.codercto.com/a/5 ...

  9. 2020中国.NET开发者峰会主题内容发布

    2020年12月09日,组委会正式发布了China .NET Conf 2020中国 .NET 开发者峰会的主题内容. 今年的大会主题收到超预期的主题,无论是数量还是质量上都比2019年有所进步,这也 ...

  10. AcWing 195. 骑士精神

    双向BFS (广搜) \(O(8 ^ 7)\) 看到没有双向BFS的题解我就过来了 这道题也可以用双向\(BFS\)来做,时间复杂度与\(IDA*\)不相上下. 双向\(BFS\)的实现有多种: 把初 ...