【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 cycle, return null.
Follow up:
Can you solve it without using extra space?
解法一:
使用unordered_map记录当前节点是否被访问过,如访问过返回该节点,如到达尾部说明无环。
/**
* 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) {
unordered_map<ListNode*, bool> visited;
while(head != NULL)
{
if(visited[head] == true)
return head;
visited[head] = true;
head = head->next;
}
return NULL;
}
};

解法二:
使用快慢指针,
fast每次前进两步(若两步中出现NULL,则返回NULL).
slow每次前进一步(若出现NULL,则返回NULL).
当出现环路,则总有某时刻,fast会赶上slow(相遇),证明如下:
1、若fast套slow圈之前在slow后方两步,则fast2slow1之后,fast在slow后方一步,进入(2)
2、若fast套slow圈之前在slow后方一步,则fast2slow1之后,fast追上slow(相遇)
其他情况均可化归到以上两步。
假设从head到环入口entry步数为x,环路长度为y,相遇时离entry的距离为m
则fast:x+ay+m,slow:x+by+m (a > b)
x+ay+m = 2(x+by+m)
整理得x+m = (a-2b)y
以上式子的含义是,相遇时的位置(m)再前进x步,正好是y的整倍数即整圈。
现在的问题是怎样计数x。
利用head到entry的长度为x,只要fast从head每次前进一步,slow从相遇位置每次前进一步,
再次相遇的时候即环的入口。
/**
* 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* fast = head;
ListNode* slow = head;
do
{
if(fast != NULL)
fast = fast->next;
else
return NULL;
if(fast != NULL)
fast = fast->next;
else
return NULL;
slow = slow->next;
}while(fast != slow);
fast = head;
while(fast != slow)
{
fast = fast->next;
slow = slow->next;
}
return slow;
}
};

【LeetCode】142. Linked List Cycle II (2 solutions)的更多相关文章
- 【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 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ...
- 【LeetCode】141. Linked List Cycle (2 solutions)
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...
- 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
题目: Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it without using ...
- 【Lintcode】103.Linked List Cycle II
题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...
- 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现
引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...
- 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 ...
随机推荐
- 用java解析在OpenStreetMap上下载的地图数据(SAX版,适合比较大的xml文件)
java程序如下: package gao.map.preprocess; import java.io.BufferedWriter; import java.io.File; import jav ...
- JTS(Geometry)(转)
原文链接:http://blog.csdn.net/cdl2008sky/article/details/7268577 空间数据模型(1).JTS Geometry model (2).ISO Ge ...
- 屏保:画线圈LineFlower
LineFlowerSP 小时候玩过一种画线圈的玩具,将一个圆形齿轮在一个大圈里转,会画出各种图形来.这个程序就是模仿它做的.算法原理:将一个圆围绕着另一个大圆公转,并且它还做自转运动.那么圆内一点的 ...
- 血淋淋的事实告诉你:你为什么不应该在JS文件中保存敏感信息
在JavaScript文件中存储敏感数据,不仅是一种错误的实践方式,而且还是一种非常危险的行为,长期以来大家都知道这一点. 而原因也非常简单,我们可以假设你为你的用户动态生成了一个包含API密钥的Ja ...
- 2018CVPR
CVPR 2018:腾讯图像去模糊.自动人像操纵最新研究 解密运动模糊:走向实用的非特定场景图片去模糊技术 在慢速曝光或快速运动拍摄照片时,图像模糊常常困扰着照片拍摄者.优图实验室的研究人员开发了可以 ...
- $.jsonp()的简单使用
// jsonp 获取 json 数据: $.jsonp({ url: GLOBAL.baseUrl + '/company/mobi_getposter.action', callback: 'ca ...
- C#版查杀本地/远程进程工具
xkill [原创] Author: R&S E-mail: yrwithsh@vip.sina.com HomePage: fz5fz.yeah.net Date: 10/04/2003 u ...
- T-SQL with as 的用法(转) SQL 下的递归查询 SQL2005(CTE) ,SQL2000(Function 递归)
摘自: http://blog.csdn.net/bluefoxev/article/details/6779794 ------- SQL2005 方法 一.WITH AS的含义 WITH ...
- 【nodejs】用express又做了份crud
感觉crud是高级形式的hello world了. app代码: 'use strict'; var express=require('express'); var http=require('htt ...
- (回溯法)和为n的所有不增正整数和式分解算法
题目: 利用递归算法输出正整数和为n的所有不增的正整数和式.例如当n=5时,不增的和式如下: 5=5 5=4+1 5=3+2 5=3+1+1 5=2+2+1 5=2+1+1+1 5=1+1+1+1+1 ...