(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: ( ...
随机推荐
- POJ 1469 COURSES 二分图最大匹配 二分图
http://poj.org/problem?id=1469 这道题我绝壁写过但是以前没有mark过二分图最大匹配的代码mark一下. 匈牙利 O(mn) #include<cstdio> ...
- BZOJ.2286.[SDOI2011]消耗战(虚树 树形DP)
题目链接 BZOJ 洛谷P2495 树形DP,对于每棵子树要么逐个删除其中要删除的边,要么直接断连向父节点的边. 如果当前点需要删除,那么直接断不需要再管子树. 复杂度O(m*n). 对于两个要删除的 ...
- hdu 2112 最短路
本来是拿来复习一下map的,没想搞了半天,一直wa,最后发现预处理没有处理到所有的点 就是个最短路 Sample Input 6 xiasha westlake xiasha station 60 x ...
- A server is already running. Check /home/peter/stock/tmp/pids/server.pid. Exiting【Xshell 运行rails s 报错】
- python开发_tkinter_图形随鼠标移动
做这个东西的时候,灵感源自于一个js效果: 两个眼睛随鼠标移动而移动 运行效果: =============================================== 代码部分: ===== ...
- IIS Express并发数设置
今天将之前的一个瓦片图的服务迁移到了asp.net core试了一下,使用的时候感觉客户端刷新时有些慢,估计是并发连接数限制的原因. 由于这是一个开发中的版本,是用IIS Express部署的,IIS ...
- systemtap 2.8 news
* What's new in version 2.8, 2015-06-17 - SystemTap has improved support for probing golang programs ...
- Xsolla和Crytek合作,对游戏战争前线推出全新支付方式
新闻稿: Sherman Oaks, 加州 (美国) –2014年 10月 15日-计费提供商Xsolla今日正式宣布.和著名游戏开发商以及发行商 Crytek.这次合作意味着玩家能够期待大量的游戏内 ...
- 33.NET对加密和解密的支持
散列运算 mscorlib.dll下的System.Security.Cryptography下: 抽象类HashAlgorithm 抽象类MD5 MD5CryptoSer ...
- 【spring cloud】【IDEA】【Maven】spring cloud多模块打包,打包的jar包只有几k,jar包无法运行,运行报错:no main manifest attribute, in /ms-eureka.jar
======================================================================================== 引申:maven打包多 ...