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 pos
which 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 ...
随机推荐
- 炒鸡讨厌换python版本呀
https://www.cnblogs.com/yjlch1016/p/8641910.html 还是说,装个 Anaconda,你好我好大家好. https://blog.csdn.net/qq_3 ...
- 关于笔记本安装parrot和kali的一些问题(花屏,息屏,屏幕不能休眠)
新入手了个笔记本,还是想跟原来一样装回熟悉的kali环境中,结果我的天啊,这一路坑,简直了. 写下我遇到的问题吧,算是给大家提供一些解决方法. 1.安装kali和parrot出现无法引导的grub的情 ...
- 3. Port scanners (端口扫描器 4个)
3. Port scanners (端口扫描器 4个) 愤怒的IP扫描器是一个小的开源Java应用程序,它执行主机发现(“ping扫描”)和端口扫描. 旧的2.x版本只有Windows,但是,新的3. ...
- 如何清除SQLServer服务器名称、登录名等
SQLServer 2008 R2清理方法: 找到下面路径:C:\Users\%username%\AppData\Roaming\Microsoft\Microsoft SQL Server\100 ...
- cmake add_custom_command 使用
cmake add_custom_command 使用 今天整理编译工程,想在编译工程前面用tolua生成c文件, 使用命令add_custom_command后,附加的命令并不执行,如下: add_ ...
- 修改windows7 的管理员密码
某天,公司财务同事的电脑出现了一个相当奇葩的现象,有些程序不能用了,经过查看,发现本是管理员的账户变得只有一个users用户组了 造成程序在运行时,无权限修改一些文件,造成程序无法启动 my god, ...
- Python学习基本小练习
对于python的10个小练习做下笔记 1.使用while循环输入1 2 3 4 5 6 8 9 10...自己写的代码如下: num1 = 0 while num1 < 10: num1 = ...
- Faster-RCNN理解
一.Faster-RCNN基本结构 该网络结构大致分为三个部分:卷积层得到高位图像特征feature maps.Region Proposal Network得到候选边框.classifier识别出物 ...
- Unity外包团队:U3D与UE我选哪个好?请别再问这种问题了!
原本预先决定的两家VR游戏公司采访,思熊和星为棋,并没有发现什么共性之初.结果在采访之后却意外发现,两家的经历有着非常相似的地方.他们都是来自于开发游戏所用的引擎的原开发商,比如思熊的主力来自Epic ...
- Ubuntu下查看SD卡设备名的几个方法
Ubuntu下使用SD卡查询SD卡的设备文件名:sudo fdisk -leg:Disk /dev/sdb:14.9 GiB,15931539456 字节,31116288 个扇区单元:扇区 / 1 ...