(LeetCode 141/142)Linked List Cycle
1、Given a linked list, determine if it has a cycle in it.
2、Given a linked list, return the node where the cycle begins. If there is no cycle, return null
.
3、Given a linked list, return the length of the cycle, if there is no cycle, return 0;
题目要求:
1、给一个链表,判断它是否存在环;
2、给一个链表,如果它存在环,请返回环的起点,否则,返回NULL;
3、给一个链表,如果它存在环,请返回环的长度,否则,返回0;
解题思路:
1、判断是否存在环?
方法1:增加一个set容器,遍历链表,将各个结点依次加入Set集合中,如果该节点已存在集合中,说明存在环;如果遍历结束后,发现没有重复的结点,则说明没有环。
方法2:无需额外空间,设置两个指针p1,p2,从头结点开始,一个走一步,p1=p1->next;,一个走两步,p2=p2->next->next,如果存在环,那么两个指针肯定会相遇;如果走得快的指针p2到达了终点,说明没有环。
2、如何求环的起点和环的长度呢?
设链表起点距离环的起点距离为a,圈长为n,当p1和p2相遇时,相遇点距离环起点距离为b,此时b已绕环走了k圈,则
p1走的距离为a+b;
p2速度为p1的两倍,p2走的距离为2*(a+b);
p2走的距离为a+b+k*n=2*(a+b),从而a+b=k*n
即当p1走a步,p2走(k*n-b)步,当k=1时,则为(n-b)步;
因此如何求环的起点?把p1拉回起点重新出发,p2从相遇点继续走,p1,p2每次均走一步,a步后,p1到达起点,p2也刚好到圈的起点。
如何求环的长度?相遇后,p2再走一圈并统计长度就是圈长。
参考代码:
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) {
set<ListNode*> psets;
for(ListNode *p=head;p!=NULL;p=p->next){
if(psets.find(p)!=psets.end())
return true;
psets.insert(p);
}
return false;
}
};
/**
* 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 || head->next==NULL)
// return false;
ListNode *p1=head;
ListNode *p2=head;
while(p2 && p2->next){
p1=p1->next;
p2=p2->next->next;
if(p1==p2)
return true;
}
return false;
}
};
2、求环的起点
/**
* 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 *p1,*p2;
p1=head;
p2=head;
while(p2 && p2->next){
p1=p1->next;
p2=p2->next->next;
if(p1==p2){
// p1 points back to head,p2 still stand where they have met
// p1,p2 take the same steps
// when they meet again, that's where the cycle starts
p1=head;
while(p1!=p2){
p1=p1->next;
p2=p2->next;
}
return p1;
}
}
return NULL;
}
};
3、求环的长度
/**
* 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 *p1,*p2;
p1=head;
p2=head;
while(p2 && p2->next){
p1=p1->next;
p2=p2->next->next;
if(p1==p2){
// if p2 cycles around,then the length of cycle can be counted
ListNode *p=p2->next;
int length=;
while(p!=p2){
length++;
p2=p2->next;
}
return length;
}
}
return ;
}
};
(LeetCode 141/142)Linked List Cycle的更多相关文章
- LeetCode 141. 环形链表(Linked List Cycle) 19
141. 环形链表 141. Linked List Cycle 题目描述 给定一个链表,判断链表中是否有环. 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 ...
- [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之“链表”: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 ...
- leetcode 141 142. Linked List Cycle
题目描述: 不用辅助空间判断,链表中是否有环 /** * Definition for singly-linked list. * struct ListNode { * int val; * Lis ...
- 已知前序(后序)遍历序列和中序遍历序列构建二叉树(Leetcode相关题目)
1.文字描述: 已知一颗二叉树的前序(后序)遍历序列和中序遍历序列,如何构建这棵二叉树? 以前序为例子: 前序遍历序列:ABCDEF 中序遍历序列:CBDAEF 前序遍历先访问根节点,因此前序遍历序列 ...
- nginx平滑升级(1.14--1.15)
查看旧版nginx编译参数 [root@localhost yum.repos.d]# nginx -V nginx version: nginx/1.14.2 built by gcc 4.8.5 ...
- 黄金矿工(LeetCode Medium难度)1129题 题解(DFS)
题目描述: 给定一个二维网络,给定任意起点与终点.每一步可以往4个方向走.要找出黄金最多的一条线路. 很明显的是要“一条路走到黑,一直下去直到某个条件停止”. 运用dfs(深度优先搜索)求解. 因为起 ...
- LeetCode Questions List (LeetCode 问题列表)- Java Solutions
因为在开始写这个博客之前,已经刷了100题了,所以现在还是有很多题目没有加进来,为了方便查找哪些没加进来,先列一个表可以比较清楚的查看,也方便给大家查找.如果有哪些题目的链接有错误,请大家留言和谅解, ...
- 【LeetCode】链表 linked list(共34题)
[2]Add Two Numbers (2018年11月30日,第一次review,ko) 两个链表,代表两个整数的逆序,返回一个链表,代表两个整数相加和的逆序. Example: Input: ( ...
随机推荐
- Codeforces Round #354 (Div. 2) A. Nicholas and Permutation 水题
A. Nicholas and Permutation 题目连接: http://www.codeforces.com/contest/676/problem/A Description Nichol ...
- weblogic安装以及异常解决方法【转】
转自:http://shenjc2008.iteye.com/blog/1461253 下载地址: http://www.oracle.com/technetwork/middleware/weblo ...
- JSON在PHP中的基本应用
从5.2版本开始,PHP原生提供json_encode()和json_decode()函数,前者用于编码,后者用于解码. 一.json_encode() 该函数主要用来将数组和对象,转换为json格式 ...
- Toast信息框
Toast组件的功能和对话框有些相似,可是使用上更简单,使用Toast组件的目的仅仅有一个,就是在屏幕上弹出一个消息窗体告知用户某个信息,并且这个窗体没有不论什么button,经过几秒钟后就会消失.假 ...
- SQLiteSpy - A fast and compact GUI database manager for SQLite
http://www.yunqa.de/delphi/doku.php/products/sqlitespy/index SQLiteSpy is a fast and compact GUI dat ...
- java 实现输出姓和名
package xiaojie; import java.util.Scanner; public class baiJiaXing { public static void main(String[ ...
- SYSCALL_DEFINE3
http://blog.csdn.net/yueyingshaqiu01/article/details/48786961
- mysql查询null异常:attempted to return null from a method with a primitive return type
select sum(deposit_amount)from tb_commission_ib_day mysql查询时报异常: attempted to return null from a met ...
- SHP文件合并
ArcGIS中合并SHP文件是一个常用的操作,下面简要讲解一下如何合并. 使用ArcGIS Tool Box(ArcGIS工具箱)中的Data Management Tools-->Genera ...
- andriod 获得MP4时长
//获得MP4时长 private int getTimeLong(String videoPath) { MediaMetadataRetriever retr = new MediaMetadat ...