标题题目地址

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. java学生简单管理系统

    1 //设一个班有n名学生,期末考试5门,编写程序评定学生奖学金 2 514 //要求打印输出一二等奖学金学生的学号,姓名和各科成绩 3 515 //总成绩超过全班总平均成绩20%一等奖学金,超过总平 ...

  2. 记录一下超大list引起oom

    1.2g的堆内存 比较大的对象20w个,oom

  3. framework中的sentinel

    引入切面: 切面+sentinel-web-servlet private void initDataSource() { String zkUrl = zaSentinelConfig.getDat ...

  4. DRF的ModelSerializer的使用

    在views中添加 from django.shortcuts import render # Create your views here. from rest_framework.views im ...

  5. java并发编程实战《七》安全性、活跃性以及性能问题

    安全性.活跃性以及性能问题 安全性问题 那什么是线程安全呢?其实本质上就是正确性,而正确性的含义就是程序按照我们期望的执行,不要让我们感到意外. 存在共享数据并且该数据会发生变化,通俗地讲就是有多个线 ...

  6. 超详细讲解mysql存储过程中的in/out/inout

    存储过程 大概定义:用一个别名来描述多个sql语句的执行过程. 最简单 delimiter // create PROCEDURE p1() begin select * from userinfo; ...

  7. 从零开始的pickle反序列化学习

    前言 在XCTF高校战疫之中,我看到了一道pickle反序列化的题目,但因为太菜了花了好久才做出来,最近正好在学flask,直接配合pickle学一下. 找了半天终于找到一个大佬,这里就结合大佬的文章 ...

  8. 学习JUC源码(1)——AQS同步队列(源码分析结合图文理解)

    前言 最近结合书籍<Java并发编程艺术>一直在看AQS的源码,发现AQS核心就是:利用内置的FIFO双向队列结构来实现线程排队获取int变量的同步状态,以此奠定了很多并发包中大部分实现基 ...

  9. JavaScript一个一维数组变为两个一维数组

    //例如[1,2,3,4,5,6,7,8]变为[[1,2,3,4],[5,6,7,8]] var arr=[1,2,3,4,5,6,7,8]; function fixedArray(arr) { v ...

  10. Jmeter(3)返回内容乱码

    一.创建jmeter实例测试百度接口返回 1.添加线程组 2.添加取样器 3.添加监听器 二.运行结果如下 返回结果中有乱码,原因是Jmeter取样器结果的编码默认为sampleresult.defa ...