方法要记住,和判断是不是交叉链表不一样

方法是将两条链表的路径合并,两个指针分别从a和b走不同路线会在交点处相遇

public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
if (headA==null||headB==null) return null;
/*
本来想用快慢指针做,把其中一个链表收尾相接,然后再用判断循环链表路口的方法
但是觉得这个easy的题目应该不会这么复杂,看了答案却是有更好的方法
求两个链表相交点的方法是:
虽然两个链表可能不一样长,但是如果将两个链表长度叠加,然后两个指针从不同的路线走
由于速度相同,路程相同,相遇的地方就是相交点
*/
ListNode a = headA,b = headB;
while (a!=b)
{
//a走完A再走B,这是一条录路线,另一条路线是b走完B再走A,到相交点时会相遇
//这里要判断a是不是null而不是a.next,因为a和b需要走到null,如果不走的话,没有交点的两个链表会死循环
a = (a==null)?headB:a.next;
b = (b==null)?headA:b.next;
}
return a;
}

[LeetCode]160. Intersection of Two Linked Lists判断交叉链表的交点的更多相关文章

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

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

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

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

  6. Java for 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 ...

  7. ✡ leetcode 160. Intersection of Two Linked Lists 求两个链表的起始重复位置 --------- java

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

  8. Java [Leetcode 160]Intersection of Two Linked Lists

    题目描述: Write a program to find the node at which the intersection of two singly linked lists begins. ...

  9. (链表 双指针) 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 ...

随机推荐

  1. 20200427_ls_正在读取目录_输入/输出错误

    环境: 在Centos7.2上挂载了一个2T的移动硬盘, 使用vim 在移动硬盘中编辑 .sh文件, wq的时候提示出错, 然后清空的文件, 可以正常wq出来 [root@localhost yido ...

  2. badboy下载

    最近新接触了badboy软件,以下是百度网盘链接,有需要可以下载. 链接:https://pan.baidu.com/s/1O4oIhx-twcaMA_fDzRQPHg提取码:7i44 二维码:

  3. springboot:读取application.yml文件

    现在开发主要使用微服务框架springboot,在springboot中经常遇到读取application.yml文件的情形. 一.概述 开发过程中经常遇到要读取application.yml文件中的 ...

  4. Model/View开发小结

    老猿Python博文目录 专栏:使用PyQt开发图形界面Python应用 老猿Python博客地址 Model/View开发是PyQt和Qt中重要的框架之一,老猿认为另外两个就是信号槽机制和事件机制, ...

  5. 第15.17节 PyQt(Python+Qt)入门学习:PyQt图形界面应用程序的事件捕获方法大全及对比分析

    老猿Python博文目录 老猿Python博客地址 按照老猿规划的章节安排,信号和槽之后应该介绍事件,但事件在前面的随笔<PyQt(Python+Qt)实现的GUI图形界面应用程序的事件捕获方法 ...

  6. Python+Qt学习随笔:PyQt中常用的事件处理函数

    在PyQt图形界面中,我们经常要捕获特定事件如鼠标按键按下.鼠标按下等事件以执行特定操作,可以通过重写组件对象的相关事件处理函数来实现相关处理,具体特定事件常用的包括如下: keyPressEvent ...

  7. ActionResult的返回类型

    ActionResult是控制器方法执行后返回的结果类型,控制器方法可以返回一个直接或间接从ActionResult抽象类继承的类型,如果返回的是非ActionResult类型,控制器将会将结果转换为 ...

  8. 百度前端技术学院-基础-day5.6

    今天学习了关于盒模型.浮动等页面布局的方法. 受到同学的启发,顺便学习了flex的布局. 还了解了一些编码的基本规则. 对我接下来的学习帮助很大. 交作业: HTML : https://github ...

  9. WPF源代码分析系列一:剖析WPF模板机制的内部实现(一)

    众所周知,在WPF框架中,Visual类是可以提供渲染(render)支持的最顶层的类,所有可视化元素(包括UIElement.FrameworkElment.Control等)都直接或间接继承自Vi ...

  10. typora软件使用指南

    Markdown学习 标题: 三级标题 四级标题 字体 hello,world! hello,world! hello,world! hello,world! 引用 选择狂神说java,走向人生巅峰 ...