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 ...
随机推荐
- 关于PHP 缓冲区: ob_star , ob_get_contents
PHP ob_star ob_get_contents 细说 作者:田园花香 关于PHP 缓冲区 ob_start: 打开输出缓冲区,当缓冲区激活时,所有来自PHP程序的非头文件信息均不会发送, ...
- 使用 whereis/which/locate 查找文件
whereis命令 whereis只能用于搜索可执行文件 (-b) , 联机帮助文件 (-m) 和源代码文件 (-s) . 它只在一个数据库中查询(/var/lib/mlocate) . 这个数据库是 ...
- RABBITMQ too many heartbeats missed
执行rabbitmqctl status | grep -A 4 file_descriptors 显示socket_used 达到 socket_limited 的值 增加socket_limi ...
- 算法题:给出一组数字,拼接一个最大的值 PHP
举例如下:'9235','42','9','5','8','32','136','343','45' 则拼接的最大的数为 : 9-9235-8-5-45-42-343-32-136 网上坑多,想了很久 ...
- ubuntu 14.04升级gcc 4.8到5.3
添加PPA库 #sudo apt-get install software-properties-common #sudo add-apt-repository ppa:ubuntu-toolchai ...
- 解决腾讯云封锁SS(shadow--socks)访问google问题
腾讯云windows主机,若使用SS(shadow--socks)采用加密算法aes-256-cfb,访问google的时候会被dropped connection. 解决办法更改ss server加 ...
- VisualSVN设置提交时必须输入日志信息
VisualSVN设置提交时必须输入日志信息 1.svn提交时强制输入提交信息 为了阻止SVN提交空日志信息和垃圾文件可以在SVN服务器端强制必须填写日志信息,这时需用到pre-commit钩子脚本. ...
- elasticsearch -- kibana安装配置
Kibana 是为Elasticsearch设计的开源分析和可视化平台,你可以使用 Kibana 来搜索,查看存储在 Elasticsearch 索引中的数据并与之交互.你可以很容易实现高级的数据分析 ...
- sunzl is not in the sudoers file.This incident will be reported
Description: [sunzl@localhost nuc900bsp$] ./install.sh sorry!you are not the root !! [sunzl@localhos ...
- 监听 input 内容 改变
html: <input class="query_str_input" placeholder="搜索您的需求" type="text&quo ...