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 ...
随机推荐
- 2019 ICPC 沈阳网络赛 J. Ghh Matin
Problem Similar to the strange ability of Martin (the hero of Martin Martin), Ghh will random occurr ...
- java关于Integer设置-128到127的静态缓存
今天在一个java群里,看到有个群友问到如下为什么第一个为true,第二个为false. System.out.println(Integer.valueOf("50")==Int ...
- 高效使用Linux快捷键
命令行操作快捷键 Ctrl+a跳到本行的行首,Ctrl+e则跳到页尾. Ctrl+u删除当前光标前面的所有文字, ctrl+k-删除当前光标后面的所有文字 Ctrl+w删除光标前的单词 Backsap ...
- 记一次ArrayList产生的线上OOM问题
前言:本以为(OutOfMemoryError)OOM问题会离我们很远,但在一次生产上线灰度的过程中就出现了Java.Lang.OutOfMemoryError:Java heap space异常,通 ...
- Spring Boot项目跳转不到jsp页面是怎么回事
SpringBoot访问不了JSP但却能进入后台 一直报错: 解决方法: 改成下面的
- laravel中打印一个sql语句
查询构造器 打印sql是发现 toSql() 不可用 所以网上搜索下 //DB::connection()->enableQueryLog(); // 开启查询日志 $user=DB::tabl ...
- OpenJudge计算概论-分数求和
/*====================================================== 1006:分数求和 总时间限制: 1000ms 内存限制: 65536kB 描述 输入 ...
- Redis Sentinel 高可用服务架构搭建
https://www.cnblogs.com/xishuai/p/redis-sentinel.html
- python 中 logging 模块的 log 函数以及坑
记录下吧,一个日志的函数,但有个坑是在调用函数时需要先将函数实例化为一个变量,否则进入某个循环时会多次刷新日志: """ 日志模块 """ ...
- Linux下-bash: Permission denied 或者 sudo: command not found 错误
有时候执行一个脚本或者运行一个可执行文件时,如执行脚本./foo.sh,会报错-bash: ./foo.sh: Permission denied,你会再试sudo ./foo.sh,发现继续报错su ...