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 ...
随机推荐
- Unity游戏开发技术的最佳实践
活动详情 作为全球规模最大的Unity开发者聚会,历年的Unite大会都成为开发者们获取Unity最新技术知识,交流开发经验,把握行业发展脉搏,体验全球前沿科技与高品质Made with Unit ...
- 20165218 2017-2018-1 《Java程序设计》第一周学习总结
20165218 2017-2018-1 <Java程序设计>第一周学习总结 教材学习内容总结 第一章.Java入门 1. Java特点 Java具有简单.面向对象.平台无关.多线程.动态 ...
- requireJs使用方法项目实例
首先,定义 main.js 和 事件处理的公共 js main.js 主要是定义引用名称和路径的对应关系 事件绑定模块: 写jsp页面: jsp中先引入 require.js 和 main.js 然 ...
- HDU 5645
DZY Loves Balls Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others ...
- ACM1558两线段相交判断和并查集
Segment set Problem Description A segment and all segments which are connected with it compose a seg ...
- Leetcode 295. 数据流的中位数
1.题目要求 中位数是有序列表中间的数.如果列表长度是偶数,中位数则是中间两个数的平均值. 例如, [2,3,4] 的中位数是 3 [2,3] 的中位数是 (2 + 3) / 2 = 2.5 设计一个 ...
- Bat 命令相关
1. bat 里面怎么sleep 等待: ping 127.0.0.1 -n 2000 > nul 2. net use 建立映射: net use Y: \\172.16.10.240\Inf ...
- bzoj 2037: [Sdoi2008]Sue的小球——dp
Description Sue和Sandy最近迷上了一个电脑游戏,这个游戏的故事发在美丽神秘并且充满刺激的大海上,Sue有一支轻便小巧的小船.然而,Sue的目标并不是当一个海盗,而是要收集空中漂浮的彩 ...
- vue清空input file
input file是只读的,给form一个id,用form.reset()干掉里面input的值 document.getElementById("uploadForm")&am ...
- js中的true和false
1.false undefined.NaN.0.null和空字符串''均被视为false 2.true 除上述以外的其它情况一律被视作true