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

方法是将两条链表的路径合并,两个指针分别从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. 2014_07_11_VGA基础及封装

    -- VR1201 Color Filter LCOS Microdisplays QVGA (320*240 Pixel) Color Filter LCOS Microdisplays -- VR ...

  2. jquery on 动态生成绑定事件

    $(document).on("mouseenter", ".v6-div-kind-ok", function () { alert();});

  3. 20190713_发布网站的时候报错:无法完成向远程代理 URL 发送请求 基础连接已经关闭 发送时发生错误

    环境介绍: Vs2017 IIS 7.5 服务器: windows 2008 R2 overflow上有一篇帖子讲了关于问题的解决办法: 链接: https://stackoverflow.com/q ...

  4. 性能测试学习之路 (二)jmeter详解(jmeter执行顺序 && 作用域 && 断言 && 事务 &&集合点 )

    1 Jmeter 工作区介绍 jmeter工作区分为3个部分:目录树.测试计划编辑区域.菜单栏. 2 Jmeter 执行顺序规则 Jmeter执行顺序规则如下: 配置元件 前置处理器 定时器 采样器s ...

  5. 漫话docker的衰落与kubernetes的兴起

    本文首发在OPPO互联网公众号,欢迎点击转载 https://mp.weixin.qq.com/s/wBC4CgAzXeTNURa1YdYmIQ. 伴随着kubernetes 1.20中对于docke ...

  6. 百度前端技术学院-基础-day20-21

    第二十到第二十一天:让你和页面对话 task1 控制元素的显示及隐藏 实现以下功能: 当用户选择了 School 的单选框时,显示 School 的下拉选项,隐藏 Company 的下拉选项 当用户选 ...

  7. vue props默认值国际化报错

    未做国际化处理 tabLabel: { type: Array, default: () => (["a", "b", "c"]) } ...

  8. js内存泄漏的问题?

    内存泄漏指任何对象在您不再拥有或需要它之后仍然存在. 垃圾回收器定期扫描对象,并计算引用了每个对象的其他对象的数量.如果一个对象的引用数量为 0(没有其他对象引用过该对象),或对该对象的惟一引用是循环 ...

  9. 题解 P5401 [CTS2019]珍珠

    蒟蒻语 这题太玄学了,蒟蒻写篇题解来让之后复习 = = 蒟蒻解 假设第 \(i\) 个颜色有 \(cnt_i\) 个珍珠. \(\sum\limits_{i=1}^{n} \left\lfloor\f ...

  10. 我的第一次shell

    我的第一次shell 最近我们的项目需要进行优化,整体架构进行改造. 然后我们红超哥就看我骨骼惊奇,说小伙子你想不想当做掌门人呀.(我说不想哈哈) 想不想也没用了,红超哥说我们现在的架构有所改变,需要 ...