53-Linked List Cycle II
- Linked List Cycle II My Submissions QuestionEditorial Solution
Total Accepted: 74093 Total Submissions: 235430 Difficulty: Medium
Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
Note: Do not modify the linked list.
Follow up:
Can you solve it without using extra space?
思路:假设快慢指针在x处相遇,
此时快指针在环内假设走了n圈加x步,而总的步数:nr+x+a
是慢指针的2倍
即有nr+x+a =(x+a)*2
上式为a=(n-1)r+r-x;
说明慢指针走到a时候,快指针从x处走了r-x前一个加上n-1圈的距离,
注意此时快指针从X处一步一步走,慢指针也一步一步走,直到在入口相遇
时间复杂度:O(n)
空间复杂度:O(1)
/**
* 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 *p_quick=head,*p_slow=head;
while(p_quick!=NULL&&p_quick->next!=NULL){
p_quick = p_quick->next->next;
p_slow = p_slow->next;
if(p_quick==p_slow)break;
}
if(p_quick==p_slow){
p_slow=head;
while(p_slow!=p_quick){
p_slow = p_slow->next;
p_quick=p_quick->next;
}
return p_slow;
}
else return NULL;
}
};
53-Linked List Cycle II的更多相关文章
- [算法][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 ...
- LeetCode之“链表”:Linked List Cycle && Linked List Cycle II
1.Linked List Cycle 题目链接 题目要求: Given a linked list, determine if it has a cycle in it. Follow up: Ca ...
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
- LeetCode Linked List Cycle II 和I 通用算法和优化算法
Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...
随机推荐
- 请问为什么要用三极管驱动mos,直接用mos有什么缺点呢?
可能无法完全导通,电流可能过小使导通所需时间变长,最终导致发热严重 回复 举报 csaaa DIY七级 3# 发表于 2016-7-12 14:11:59 直接驱动mos也没什么问 ...
- 单片机零基础学习之从“点灯”入门STM32
本篇文章通过一个简单的例子来熟悉模块化编程以及利用库函数的方法进行开发使用STM32外设的基本流程. 首先,我们打开本讲的例程,在工程目录我们可以看到驱动分组下有 led.delay 两个.c源文件, ...
- Flutter应用在夜神模拟器启动白屏问题
Flutter应用在夜神模拟器启动白屏问题 flutter run 出现如下错误 [ERROR:flutter/shell/gpu/gpu_surface_gl.cc(39)] Failed to ...
- Nginx(三):Linux环境(Ubuntu)下Nginx的安装
Nginx 是一位俄罗斯人 Igor Sysoev(伊戈尔·塞索斯夫)编写的一款高性能HTTP和反向代理服务器. Nginx 主要是有C编写的,安装Nginx需要GCC编译器(GNU Compiler ...
- NOIP模拟86(多校19)
T1 特殊字符串 解题思路 \(f_{i,j}\) 表示前 \(i\) 个字符中结尾为 \(j\) 的最大贡献. 转移枚举当前位置于之前位置结尾的组合加上贡献即可. 对于边界问题,容易发现选择 1 一 ...
- linux中dd命令
转载:https://www.runoob.com/linux/linux-comm-dd.html Linux dd 命令用于读取.转换并输出数据. dd 可从标准输入或文件中读取数据,根据指定的格 ...
- (总结)Linux下su与su -命令的本质(转)
转载地址:http://www.ha97.com/4001.html 本人以前一直习惯直接使用root,很少使用su,前几天才发现su与su -命令是有着本质区别的! 大部分Linux发行版的默认账户 ...
- Jenkins执行 remote SSH 命令
1.安装 SSH Pipeline Steps 插件 2.在凭据中添加remote server凭据,如下 3.Pipeline编写: def GetRemoteServer(ip){ def rem ...
- 手把手教你学Dapr - 7. Actors
上一篇:手把手教你学Dapr - 6. 发布订阅 介绍 Actor模式将Actor描述为最低级别的"计算单元".换句话说,您在一个独立的单元(称为actor)中编写代码,该单元接收 ...
- 菜鸡的Java笔记 - java 常用类库
CommonClassLibrary 常用类库 定时调度 定时调度指的是每到一个时刻,都会自动的产生某些特定的操作形式 con ...