[LeetCode]160. Intersection of Two Linked Lists判断交叉链表的交点
方法要记住,和判断是不是交叉链表不一样
方法是将两条链表的路径合并,两个指针分别从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判断交叉链表的交点的更多相关文章
- [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 ...
- [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 ...
- 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 ...
- 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 ...
- [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 ...
- 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 ...
- ✡ 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 ...
- 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. ...
- (链表 双指针) 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 ...
随机推荐
- JZOJ【NOIP2013模拟联考14】隐藏指令
JZOJ[NOIP2013模拟联考14]隐藏指令 题目 Description 在d维欧几里得空间中,指令是一个长度为2N的串.串的每一个元素为d个正交基的方向及反方向之一.例如,d = 1时(数轴) ...
- 嘶吼CTF easy calc
进入之后可以看到我们需要输入一个计算式来得到答案,burpsuite进行抓包之后发现页面来自于calc.php 我们直接访问calc.php页面 发现源代码泄露 可以看到当我们没有输入num值的时候就 ...
- 深入理解C#中的异步(一)——APM模式EAP模式
深入理解C#中的异步(一)--APM模式EAP模式 目录 深入理解C#中的异步(一)--APM模式EAP模式 1 使用异步编程的原因 2 异步编程模式 2.1 APM模式 2.1.1 APM模式示例代 ...
- POJ2466 棋盘覆盖
一张\(n*m\)的棋盘,有\(k\)个点不能被覆盖,问其余点能不能被\(1*2\)的小矩形完全覆盖,多测 这题先输入\(m\)是什么鬼啊!!! 其实是一个比较裸的二分图判定,把\(k\)个点挖去然后 ...
- Codeforces Edu Round 62 A-E
A. Detective Book 模拟题,有一些细节需要注意. #include <cstdio> #include <iostream> #include <cmat ...
- 题解-Cats Transport
题解-Cats Transport Cats Transport 有 \(n\) 个山丘,\(m\) 只猫子,\(p\) 只铲屎官.第 \(i-1\) 个山丘到第 \(i\) 个山丘的距离是 \(d_ ...
- sort by背后使用了什么排序算法
用到了快速排序,但不仅仅只用了快速排序,还结合了插入排序和堆排序 搬运自https://blog.csdn.net/qq_35440678/article/details/80147601
- 恕我直言,你可能连 GitHub 搜索都不会用 - 如何精准搜索的神仙技巧
大家好,我是你们的 前端章鱼猫,一个不喜欢喵.又不喜欢吃鱼的超级猫 ~ 今天给大家带来的是 在 GitHub 上如何精准搜索的神仙技巧. [前端GitHub:https://github.com/bi ...
- 详解双向链表的基本操作(C语言)
@ 目录 1.双向链表的定义 2.双向链表的创建 3.双向链表的插入 4.双向链表的删除 5.双向链表更改节点数据 6.双向链表的查找 7.双向链表的打印 8.测试函数及结果 1.双向链表的定义 上一 ...
- 庐山真面目之八微服务架构 NetCore 基于 Dockerfile 文件部署
庐山真面目之八微服务架构 NetCore 基于 Dockerfile 文件部署 一.简介 从今天开始,不出意外的话,以后所写的文章中所介绍项目的部署环境都应该会迁移到Linux环境上,而且是 ...