<LeetCode OJ> 141 / 142 Linked List Cycle(I / II)
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
分析:
假设有环?遍历链表将无法走完,假设无环终会走到尾为NULL的位置
让一个指针每次走一个,一个指针每次走两个位置。
假设当中一个为NULL则无环。
假设相遇(必会相遇)了则有环。
time,o(n),space,o(1)
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/ class Solution {
public:
bool hasCycle(ListNode *head) {
if(head==NULL)
return false;
ListNode *showNode=head;
ListNode *fastNode=head;
while(true)
{
if(showNode->next!=NULL)
showNode=showNode->next;
else
return false;
if(fastNode->next!=NULL && fastNode->next->next!=NULL)
fastNode=fastNode->next->next;
else
return false;
if(showNode==fastNode)
return true;
}
return false;
}
};
别人的简洁算法:一样的思路
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *slow=head,*fast=head;
while(slow&&fast&&fast->next){
slow=slow->next; //跑的慢
fast=fast->next->next; //跑的快
if(slow==fast) return true; //相遇则有环
}
return false;
}
};
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?
分析:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" style="border:none; max-width:100%">
1). 使用快慢指针法,若链表中有环,能够得到两指针的交点M
2). 记链表的头节点为H,环的起点为E 2.1) L1为H到E的距离
2.2) L2为从E出发,首次到达M时的路程
2.3) C为环的周长
2.3) n为快指针在环中所绕圈数 依据L1,L2和C的定义,我们能够得到:
慢指针行进的距离为L1 + L2
快指针行进的距离为L1 + L2 + n*C 因为快慢指针行进的距离有2倍关系,因此:
2 * (L1+L2) = L1 + L2 + n*C => L1 + L2 = n*C => L1 = (n-1)*C+ C - L2
能够推出H到E的距离 = 从M出发绕n-1圈后到达E时的路程
因此,当快慢指针在环中相遇时,我们再令一个慢指针从头节点出发
接下来当两个慢指针相遇时,即为E所在的位置
/**
* 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) {
ListNode *slow=head,*fast=head;
while(slow && fast && fast->next){
slow=slow->next; //跑的慢
fast=fast->next->next; //跑的快
if(slow==fast) break; //相遇则有环
}
if(fast==NULL || fast->next==NULL)
return NULL; fast=head;
while(slow != fast){
slow=slow->next; //一样的速度跑
fast=fast->next;
}
return slow;
}
};
注:本博文为EbowTang原创,兴许可能继续更新本文。
假设转载。请务必复制本条信息!
原文地址:http://blog.csdn.net/ebowtang/article/details/50507131
原作者博客:http://blog.csdn.net/ebowtang
本博客LeetCode题解索引:http://blog.csdn.net/ebowtang/article/details/50668895
<LeetCode OJ> 141 / 142 Linked List Cycle(I / II)的更多相关文章
- [LC]141题 Linked List Cycle (环形链表)(链表)
①中文题目 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始). 如果 pos 是 -1,则在该链表中没有环. 示例 ...
- [LeetCode] 141&142 Linked List Cycle I & II
Problem: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without ...
- leetcode 141 142. Linked List Cycle
题目描述: 不用辅助空间判断,链表中是否有环 /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...
- LeetCode 141. Linked List Cycle (链表循环)
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- LeetCode OJ:Linked List Cycle(链表循环)
Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ext ...
- LeetCode算法题-Linked List Cycle(Java实现)
这是悦乐书的第176次更新,第178篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第35题(顺位题号是141).给定一个链表,确定它是否有一个循环. 本次解题使用的开发工 ...
- <LeetCode OJ> 26 / 264 / 313 Ugly Number (I / II / III)
Write a program to check whether a given number is an ugly number. Ugly numbers are positive numbers ...
- LeetCode OJ:Maximum Depth of Binary Tree(二叉树最大深度)
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- Linked List Cycle(链表成环)
判断链表中是否有环 来源:https://leetcode.com/problems/linked-list-cycle Given a linked list, determine if it ha ...
随机推荐
- Python(2)-- 运算符
1. 算术运算符 常规: 加(+).减(-).乘(*).除(/).取模(%) 此外: 幂(**):返回x的y次幂, eg: 2**3---返回 2 的 5 次幂,输出结果32 取整除(//):返回商的 ...
- 标准IO与文件IO 的区别
先来了解下什么是标准IO以及文件IO. 标准IO:标准I/O是ANSI C建立的一个标准I/O模型,是一个标准函数包和stdio.h头文件中的定义,具有一定的可移植性.标准IO库处理很多细节.例如缓存 ...
- 【BZOJ4476&JSOI2015】送礼物(二分,RMQ)
ANS明显是有二分性的 二分答案,设二分值为b M(i,j)−m(i,j)j−i+k>b 显然当l<长度<r时,一端是最小值,一端是最大值. 等于l或r的时候因为可能不满足以上性质, ...
- 不用框架使用ajax 纯js使用ajax post,get范例及其区别
原文发布时间为:2009-11-15 -- 来源于本人的百度文章 [由搬家工具导入] 不用框架使用ajax 纯js使用ajax post,get范例及其区别 ===================== ...
- event.srcElement就是指向触发事件的元素,他是什么就有什么的属性
原文发布时间为:2009-06-29 -- 来源于本人的百度文章 [由搬家工具导入] 得到或设置触发事件的对象。 event.srcElement就是指向触发事件的元素,他是什么就有什么的属性 s ...
- 小甲鱼PE详解之资源(PE详解11)
原文出自:www.fishc.com 最近一直在安排第一届鱼C 学习班的事情,忙活了好一阵子,真是对不住大家,还大家久等了,这里要跟大家说声不好意思 ^_^ 今天我们来谈谈资源部分,资源部分可以说是 ...
- JavaScript-性能优化,函数节流(throttle)与函数去抖(debounce)
我在写一个类似百度搜索框的自动提示功能时候,使用了AJAX+keydown事件.调试时候我发现,当在搜索框中输入文字的时候,控制台在不停发送AJAX.这在本地服务器测试还好,如果我把它拿到运行环境,很 ...
- 【linux高级程序设计】(第八章)进程管理与程序开发 5
守候进程 启动方式: 在系统启动时由/etc/rd.d目录下的启动脚本启动 利用inetd超级服务器启动 有cron命令定时启动,以及在终端用nohup命令启动 守护进程编程要点 (1)屏蔽有关控制终 ...
- github 获取repo 发布的版本号
获取最新版本 https://api.github.com/repos/nickchou/paopao/releases/latest 获取版本列表 https://api.github.com/re ...
- 三、Ubuntu 使用Xshell
1.下载一个Xshell 2.输入命令:ssh root@Ubuntu的ip地址 输入密码,即可连接 如果遇到不可连接.在Ubuntu中输入命令 vim /etc/ssh/sshd_config 更 ...