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

方法是将两条链表的路径合并,两个指针分别从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. LeetCode 046 Permutations

    题目要求:Permutations(全排列) Given a collection of numbers, return all possible permutations. For example, ...

  2. LeetCode 020 Valid Parentheses

    题目描述:Valid Parentheses Given a string containing just the characters '(', ')', '{', '}', '[' and ']' ...

  3. Python_爬虫养殖专业户_00

    为什么学习爬虫? 当你在夜深人静,睡不着觉,想看一些更加睡不着觉的图片/视频时... 这是一句疑似玩笑话, 现实情况是, 每一天, 整个社会都积累了大量的数据, 在数据化的社会中,没有大批量的收集和探 ...

  4. MySQL索引(一)索引基础

    索引是数据库系统里面最重要的概念之一.一句话简单来说,索引的出现其实是为了提高数据查询的效率,就像书的目录一样. 常见模型 索引的出现是为了提高查询效率,但是实现索引的方式却有很多种,这里就介绍三种常 ...

  5. PyQt(Python+Qt)学习随笔:Qt Designer中spacer部件的sizeHint属性

    在两种Spacer部件中都有sizeHint属性,在<PyQt(Python+Qt)学习随笔:Qt Designer中部件的三个属性sizeHint缺省尺寸.minimumSizeHint建议最 ...

  6. postman学习网址

    postman使用详解: http://gold.xitu.io/entry/57597a62a341310061337885 https://www.getpostman.com/docs/writ ...

  7. P5838 [USACO19DEC]Milk Visits G

    发现是一道比较裸的树上莫队,于是就开始刚,然后发现好像是最难的一道题--(本题解用于作者加深算法理解,也欢迎各位的阅读) 题意 给你一棵树,树有点权,询问一条路径上是否有点权为 \(c\) 的点. 题 ...

  8. 操作系统精髓与设计原理(九)——I/O管理和磁盘调度

    文章目录 I/O设备 I/O功能组织 直接存储器访问 操作系统设计问题 设计目标 IO功能的逻辑结构 I/O缓冲 单缓冲 双缓冲 循环缓冲 缓冲的作用 磁盘调度 磁盘性能参数 磁盘调度策略 先进先出 ...

  9. mysql 基础入门 单表查询

    单表查询 select 表头,表头 as 别名 ,表头(+-*/的运算) from table_a 1.条件查询 where + 条件 <> , != 不等于 = 等于,也可以表示字符串值 ...

  10. 00-JAVA语法基础

    1. 原码为数的二进制数,反码是将其二进制数每一位按位取反.补码则不同,正数的补码是其原码本身,负数的补码是其除符号位以外其他每一位按位取反再加一,符号位不变. int a=100; a=a>& ...