LC 417. Linked List Cycle II
题目描述
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 = [3,2,0,-4], pos = 1
Output: tail connects to node index 1
Explanation: There is a cycle in the linked list, where tail connects to the second node.

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

参考答案
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
if(head == NULL || head->next == NULL) return NULL;
ListNode* fp = head;
ListNode* sp = head;
bool isCycle = false; while(fp != NULL && sp != NULL){
fp = fp -> next;
if(sp -> next == NULL) return NULL;
sp = sp->next->next;
if(fp == sp) {
isCycle = true;
break;
}
} if(!isCycle) return NULL; fp = head;
while(fp != sp) {
fp = fp->next;
sp = sp->next;
}
return fp; }
};
答案注释
想象 fast 的速度是一步,slow 的速度是不动。那么,常规情况下,他俩在a点集合,在a点再次相遇。
但由于slow 摸鱼了,晚来了,fast已经走到b点了,slow才和fast集合,一起出发。a-b 就是所求的 循环开始点 到 列表开始点的距离。
更详细解释,可参考:https://www.cnblogs.com/hiddenfox/p/3408931.html
LC 417. Linked List Cycle II的更多相关文章
- [LC] 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: Linked List Cycle II 解题报告
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...
- 15. Linked List Cycle && Linked List Cycle II
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you solve i ...
- 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解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- 【LeetCode练习题】Linked List Cycle II
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- [Linked List]Linked List Cycle,Linked List Cycle II
一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...
- Linked List Cycle && Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
随机推荐
- 如何设置xshell代理?
场景:我想在公司内部用一台服务器A访问客户内网的机器C.在公司和客户之间有一台中间服务器B,我只能先连接到中间服务器,然后通过中间服务器跳转才能到客户C机器. 上面场景的连接策略:A->B-&g ...
- linux下安装apache和php和mysql
我的系统环境时ubuntu 18.04.3,为了ROS: 首先:安装下面一堆软件包: sudo apt install nginx nginx-doc fcgiwrap sudo apt instal ...
- linux too many files
Too many open files这个问题主要指的是进程企图打开一个文件,或者叫句柄,但是现在进程打开的句柄已经达到了上限,已经无法打开新句柄了. 网上一提到这个问题就要增加句柄上限,而往往这种情 ...
- sql查询表的所有字段和表字段对应的类型
1.查询表的所有字段 select syscolumns.name from syscolumns where id=object_id('写上要查询的表名') 2.查询表的所有字段+表字段对应的类型 ...
- 39 Flutter仿京东商城项目 收货地址列表、增加 修改收货地址布局、弹出省市区选择器
加群452892873 下载对应39课文件,运行方法,建好项目,直接替换lib目录 pubspec.yaml city_pickers: ^ AddressAdd.dart import 'packa ...
- SortedMap和TreeMap有什么区别?
SortedMap和TreeMap有什么区别 答: TreeMap的类的源码: public class TreeMap<K,V> extends AbstractMap<K,V ...
- bat命令编写大全
bat命令编写大全 摘自:https://blog.csdn.net/haibo19981/article/details/52161653 2016年08月09日 12:26:31 爱睡觉的猫L 阅 ...
- Mybase解决保存文件后再打开不能修改编辑
1.问题复现 2.解决方式 3.可以修改编辑
- web框架之MVC/MTV
MVC框架 MVC 是一种使用 MVC(Model View Controller 模型-视图-控制器)设计创建 Web 应用程序的模式 Model(模型)表示应用程序核心(比如数据库记录列表) Vi ...
- 配置Apache控制浏览器端的缓存的有效期
这个非常有用的优化,mod_expires可以减少20-30%左右的重复请求,让重复的用户对指定的页面请求结果都CACHE在本地,根本不向服务器发出请求.但要注意更新快的文件不要这么做.这个模块控制服 ...