141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目:
141.Given a linked list, determine if it has a cycle in it.
142.Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
思路:

带环链表如图所示。设置一个快指针和一个慢指针,快指针一次走两步,慢指针一次走一步。快指针先进入环,慢指针后进入环。在进入环后,可以理解为快指针追赶慢指针,由于两个指针速度相差1,则最终会相遇。
当快指针和慢指针相遇时,证明链表中存在环。141可解。
假设两个指针在C点相遇,此时慢指针走过的路程为AB+BC,快指针走过的路程为AB+BC+CB+BC。由于时间相同,快指针的速度为慢指针的2倍,则快指针走过的路程为慢指针的2倍。因此,2*(AB+BC)=AB+BC+CB+BC,得到AB=CB。
由此可以得到两个结论:
- 环的长度即为AB+BC,即A到C的长度。
- 当快指针和慢指针相遇后,把快指针放置于A点(即链表头部),慢指针仍在C点。此时两个指针一次走一步,相遇的节点即为环的起点。142可解。
代码:
141. Linked List Cycle
struct ListNode {
int val;
ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
bool hasCycle(ListNode *head) {
ListNode *fast = head;
ListNode *slow = head;
while ((fast != NULL) && (slow != NULL) && (fast->next != NULL)) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {
return true;
}
}
return false;
}
};
142. Linked List Cycle II
struct ListNode {
int val;
ListNode *next;
ListNode(int x) :
val(x), next(NULL) {
}
};
class Solution {
public:
ListNode *detectCycle(ListNode *head) {
ListNode *fast = head;
ListNode *slow = head;
while ((fast != NULL) && (slow != NULL) && (fast->next != NULL)) {
fast = fast->next->next;
slow = slow->next;
if (fast == slow) {
fast = head;
while (fast != slow) {
fast = fast->next;
slow = slow->next;
}
return fast;
}
}
return NULL;
}
};
141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)的更多相关文章
- [剑指offer]14-1.剪绳子
14-1.剪绳子 方法一 动态规划 思路:递归式为f(n)=max(f(i), f(n-i)),i=1,2,...,n-1 虽然我现在也没有彻底明白这个递归式是怎么来的,但用的时候还是要注意一下.f( ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点)
203题是在链表中删除一个固定的值,83题是在链表中删除重复的数值,但要保留一个:82也是删除重复的数值,但重复的都删除,不保留. 比如[1.2.2.3],83题要求的结果是[1.2.3],82题要求 ...
- C++版 - 剑指offer 面试题16:反转链表(Leetcode 206: Reverse Linked List) 题解
面试题16:反转链表 提交网址: http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca?tpId=13&tqId= ...
- Linked List Cycle leetcode II java (寻找链表环的入口)
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...
- 剑指Offer 1-41 代码(python实现)
今天主要写了一下offer 1-41题,余下的稍后整理 1 """ 1 镜像二叉树: 递归 """ def mirror(root): if ...
- leetcode 141. Linked List Cycle 、 142. Linked List Cycle II
判断链表有环,环的入口结点,环的长度 1.判断有环: 快慢指针,一个移动一次,一个移动两次 2.环的入口结点: 相遇的结点不一定是入口节点,所以y表示入口节点到相遇节点的距离 n是环的个数 w + n ...
- 142. Linked List Cycle II【easy】
142. Linked List Cycle II[easy] Given a linked list, return the node where the cycle begins. If ther ...
- [LeetCode] 142. Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. To r ...
随机推荐
- CodeForces 459C Pashmak and Buses(构造)题解
题意:n个人,k辆车,要求d天内任意两人都不能一直在同一辆车,能做到给出构造,不能输出-1 思路:我们把某一个人这d天的车号看成一个d位的数字,比如 1 1 2 3代表第一天1号车.第二天1号车.第三 ...
- Kylin知识点介绍
Kylin is an open source Distributed Analytics Engine from eBay Inc.that provides SQL interface and m ...
- RabbitMQ学习之延时队列
原帖参考:http://www.cnblogs.com/telwanggs/p/7124687.html?utm_source=itdadao&utm_medium=referral http ...
- PHP 内置函数fgets读取文件
php fgets()函数从文件指针中读取一行 语法: fgets(file,length) 参数 描述 file 必需.规定尧要读取的文件 length 可选 .规定尧都区的字节数.默认是102字 ...
- springmvc后台接前端的参数,数组,集合,复杂对象等
springmvc后台接前端的参数,数组,集合,复杂对象等 参考地址:https://blog.csdn.net/feicongcong/article/details/54705933 常用的几种 ...
- P3853 [TJOI2007]路标设置
传送门 思路: 类似于数列分段的二分查找答案.设目前的 mid 是一个最小的“空旷指数”,那么在 sum 数组(路标数组)里每两个相邻间的路标距离一定要小于等于目前的 mid , 如果大于,那就必须使 ...
- P1547 Out of Hay
传送门 练习 只是一个最小生成树的水题,拿来练练模板 AC代码: #include<iostream> #include<cstdio> #include<alg ...
- 《深入理解JVM虚拟机》读书笔记
前言:<深入理解JVM虚拟机>是JAVA的经典著作之一,因为内容更偏向底层,所以之前一直没有好好的阅读过.最近因为刚好有空,又有了新目标.所以打算和<构架师的12项修炼>一起看 ...
- leecode第五十九题(螺旋矩阵 II)
class Solution { public: vector<vector<int>> generateMatrix(int n) { )//特殊情况 { vector< ...
- 《剑指offer》第六十三题(股票的最大利润)
// 面试题63:股票的最大利润 // 题目:假设把某股票的价格按照时间先后顺序存储在数组中,请问买卖交易该股 // 票可能获得的利润是多少?例如一只股票在某些时间节点的价格为{9, 11, 8, 5 ...