leetcode 141 142. Linked List Cycle
题目描述:

不用辅助空间判断,链表中是否有环
/**
* 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 *first = head;
ListNode *Second = head;
while(Second->next != NULL){ first = first->next;
Second = Second->next->next;
if(Second == NULL)
return false;
if(Second == first)
return true;
}
return false;
}
};
142 找到环开始的结点

第一次相遇时slow走过的距离:a+b,fast走过的距离:a+b+c+b。
因为fast的速度是slow的两倍,所以fast走的距离是slow的两倍,有 2(a+b) = a+b+c+b,可以得到a=c(这个结论很重要!)。
/**
* 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)
return NULL;
ListNode *first = head;
ListNode *second = head;
bool flag = true;
while(second->next != NULL){
first = first->next;
second = second->next->next;
if(second == NULL)
return NULL;
if(first == second){
flag = false;
break;
}
}
if(flag == true)
return NULL;
first = head;
while(first != second){
first = first->next;
second = second->next;
}
return first; }
};
leetcode 141 142. Linked List Cycle的更多相关文章
- [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】142. Linked List Cycle II
		Difficulty:medium More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ... 
- 【LeetCode】142. Linked List Cycle II (2 solutions)
		Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ... 
- <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 ex ... 
- 【LeetCode】142. Linked List Cycle II 解题报告(Python & C++)
		作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ... 
- LeetCode OJ 142. Linked List Cycle II
		Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ... 
- leetcode 141、Linked list cycle
		一种方法是用set存储出现过的指针,重复出现时就是有环: class Solution { public: bool hasCycle(ListNode *head) { set<ListNod ... 
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
		引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ... 
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
		判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ... 
随机推荐
- bzoj1867: [Noi1999]钉子和小球(DP)
			一眼题...输出分数格式才是这题的难点QAQ 学习了分数结构体... #include<iostream> #include<cstring> #include<cstd ... 
- iostat和iowait详细解说
			简单的说,sar -u看出来的cpu利用率iowait 不实用,iostat -x 中的 svctm 和util 参数 命令形式: iostat -x 1 每隔一秒输出下 其中的svctm参数代表 ... 
- Scrapy中的Callback如何传递多个参数
			在scrapy提交一个链接请求是用 Request(url,callback=func) 这种形式的,而parse只有一个response参数,如果自定义一个有多参数的parse可以考虑用下面的方法实 ... 
- SQL Server 代理(已禁用代理 XP)
			sp_configure 'show advanced options', 1; GO RECONFIGURE WITH OVERRIDE; GO sp_configure 'Agent XPs', ... 
- 通过eclipse mybatis generater代码生成插件自动生成代码
			Mybatis属于半自动ORM,在使用这个框架中,工作量最大的就是书写Mapping的映射文件,由于手动书写很容易出错,我们可以利用Mybatis-Generator来帮我们自动生成文件.通过在Ecl ... 
- 1-shell学习(bash)
			1.为什么需要学习shell: (1)通用性,基本上所有的linux机器都会支持 (2)文字传输操作更快 (3)以后的系统管理需要使用 2.知识点: (1)变量相关: 
- [洛谷P1527] [国家集训队]矩阵乘法
			洛谷题目链接:[国家集训队]矩阵乘法 题目背景 原 <补丁VS错误>请前往P2761 题目描述 给你一个N*N的矩阵,不用算矩阵乘法,但是每次询问一个子矩形的第K小数. 输入输出格式 输入 ... 
- Tomcat报错:Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/JFreeChartTest]]
			最好把项目移除,然后在tomcat的webapps发布路径下也把项目文件删掉,重新部署就好了,原因是可能在tomcat的remove覆盖中以前的文件有所保留导致冲突,亲测有效 
- 20155335俞昆《java程序设计》第10周总结
			学号 2016-2017-2 <Java程序设计>第十周学习总结 ## 事实上网络编程,我们可以简单的理解为两台计算机相互通讯数据而已,对于程序员而言,掌握一种编程接口并使用一种编程模型相 ... 
- jQuery清空表单方法
			$(':input', '#form1') .not(':button, :submit, :reset, :hidden') .val('') .removeAttr('checked') .rem ... 
