LeetCode 142. Linked List Cycle II 判断环入口的位置 C++/Java
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
To represent a cycle in the given linked list, we use an integer poswhich represents the position (0-indexed) in the linked list where tail connects to. If pos is -1, then there is no cycle in the linked list.
Note: Do not modify the linked list.
Example 1:
Input: head = [,,,-], pos =
Output: tail connects to node index
Explanation: There is a cycle in the linked list, where tail connects to the second node.

Example 2:
Input: head = [,], pos =
Output: tail connects to node index
Explanation: There is a cycle in the linked list, where tail connects to the first node.

Example 3:
Input: head = [], pos = -
Output: no cycle
Explanation: There is no cycle in the linked list.

这题解题的思路在于:在第一次相遇点位置pos,从该位置到环入口Join的距离=从头结点Head到环入口Join的距离

假设环的长度是r,在第一次相遇时,慢指针走过的路程:s=lenA+x,快指针走过的路程:2s=lenA+nr+x,所以:lenA+x=nr,即:LenA=nr-x。
所以在第一次相遇之后,一个指针从head走到join的路程,另一个指针从pos走到join。
方法一(C++)
ListNode *detectCycle(ListNode *head) {
ListNode* slow=head,*fast=head;
while(fast&&fast->next){
slow=slow->next;
fast=fast->next->next;
if(slow==fast)
break;
}
if(!fast||!fast->next)
return NULL;
slow=head;
while(slow!=fast){
slow=slow->next;
fast=fast->next;
}
return slow;
}
(java):
ListNode slow=head,fast=head;
while(fast!=null&&fast.next!=null){
slow=slow.next;
fast=fast.next.next;
if(slow==fast)
break;
}
if(fast==null||fast.next==null)
return null;
slow=head;
while(slow!=fast){
slow=slow.next;
fast=fast.next;
}
return slow;
}
LeetCode 142. Linked List Cycle II 判断环入口的位置 C++/Java的更多相关文章
- Java for LeetCode 142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] 142. Linked List Cycle II 链表中的环 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- (链表 双指针) leetcode 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
- leetcode 142. Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- leetcode 142. Linked List Cycle II ----- java
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- leetcode 142. Linked List Cycle II 环形链表 II
一.题目大意 https://leetcode.cn/problems/linked-list-cycle-ii/ 给定一个链表的头节点 head ,返回链表开始入环的第一个节点. 如果链表无环,则 ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
随机推荐
- JS如何充分“压榨”浏览器
不同浏览器厂商实现的 JS 标准有所不同,这意味着 window 对象和可用的 api 也有所不同,希望不久的将来,所有浏览器都能实现统一的 JS 标准. 自己写 api 是很耗费时间跟精力的,而且变 ...
- QT 读写配置文件 .ini
高端大气上档次!码住 读取配置文件: //根据目录寻找配置文件 QSettings* setting = new QSettings("configs/config.ini", Q ...
- 关于mysql中存储json数据的读取问题
在mysql中存储json数据,字段类型用text,java实体中用String接受. 返回前端时(我这里返回前端的是一个map),为了保证读取出的数据排序错乱问题,定义Map时要用LinkedHas ...
- cookie 就是一些字符串信息
什么是 Cookie “cookie 是存储于访问者的计算机中的变量.每当同一台计算机通过浏览器请求某个页面时,就会发送这个 cookie.你可以使用JavaScript 来创建和取回cookie 的 ...
- java数据结构之HashSet和HashMap(java核心卷Ⅰ读书笔记)
增加 删除 remove方法,可以删除指定的一个元素. 查找 ********************* **************************** HashSet既不可以用 0 1 2 ...
- 使用xheditor时 cloneRange错误 ext.net
使用ext.net 加 xheditor时,一直报 cloneRange错误. 于是 按照说明但独使用xheditor ,检查无错,正常使用, 因此排除版本问题. <ext:panel ru ...
- linux与C内存管理机制
转自知乎专栏:https://zhuanlan.zhihu.com/p/51855842?utm_source=wechat_session&utm_medium=social&utm ...
- IE6设置li的float:left,不能自适应宽的解决方法
原文地址:https://blog.csdn.net/u012299002/article/details/50589453 做个divcss页面,发现在IE6下,设置了li的float:left,L ...
- 20175311胡济栋 2018-2019-2《Java程序设计》结对编程项目-四则运算 第二周 阶段性总结
20175311胡济栋 2018-2019-2<Java程序设计>结对编程项目-四则运算 第二周 阶段性总结 需求分析 这是利用栈来设计一个计算器的第二阶段总结. 自动生成四则运算的题目( ...
- Spring4.0开始的泛型依赖
参考资料: http://www.cnblogs.com/solverpeng/p/5687304.html 视频地址:https://edu.51cto.com/course/1956.html 一 ...