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)的更多相关文章

  1. 【LeetCode】142. Linked List Cycle II

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Given a linked list, return the node where t ...

  2. 【LeetCode】142. Linked List Cycle II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 set 日期 题目地址:https://le ...

  3. 【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 ...

  4. 【LeetCode】141. Linked List Cycle 解题报告(Java & Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 双指针 保存已经走过的路径 日期 [LeetCode ...

  5. 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 ...

  6. 【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 ...

  7. 【Lintcode】103.Linked List Cycle II

    题目: Given a linked list, return the node where the cycle begins. If there is no cycle, return null. ...

  8. 【算法分析】如何理解快慢指针?判断linked list中是否有环、找到环的起始节点位置。以Leetcode 141. Linked List Cycle, 142. Linked List Cycle II 为例Python实现

    引入 快慢指针经常用于链表(linked list)中环(Cycle)相关的问题.LeetCode中对应题目分别是: 141. Linked List Cycle 判断linked list中是否有环 ...

  9. 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 ...

随机推荐

  1. 可操纵网页URL地址的js插件-url.js

    url.js是一款能够很有用方便的操纵网页URL地址的js插件.通过url.js你能够设置和获取当前URL的參数,也能够对当前URL的參数进行更新.删除操作.还能够将当前URL的參数显示为json字符 ...

  2. Vulkan --vulkan in powervr

    zhankeng 跨平台 多线程 low cpu overhead object orientated vulkan有利于tile based的地方 明确依赖声明 细粒度同步 render passe ...

  3. 错误: 找不到或无法加载主类 Files\red5-server ,原因与解决办法

    因为你把 red5放到了 Program Files 下,而Program Files 中间有个空格,启动路径不允许有空格,换个没空格的路径就OK啦

  4. .NET MVC自定义错误处理页面的方法

    在ASP.NET MVC中,我们可以使用HandleErrorAttribute特性来具体指定如何处理Action抛出的异常.只要某个Action设置了HandleErrorAttribute特性,那 ...

  5. 老三星手机i9001刷机记录

    家里的老的三星i9001,准备给我妈用,打算刷机,但又实在头疼那些复杂的刷机技术,昨天研究了一下,用比较简单的方法完成刷机,记录如下: 用卡刷比较简单,线刷不考虑 进入恢复模式的方法:1.电源+音量加 ...

  6. C++的基本类型

  7. Codeforces#86D Powerful array(分块暴力)

    Description An array of positive integers a1, a2, ..., an is given. Let us consider its arbitrary su ...

  8. 开源 免费 java CMS - FreeCMS1.5-数据对象-job

    下载地址:http://code.google.com/p/freecms/ job 从FreeCMS 1.5 开始支持 在使用职位相关标签时,标签会封装job供页面调用. 属性 说明 id id s ...

  9. php之快速入门学习-4(数据类型)

    PHP 5 数据类型 String(字符串), Integer(整型), Float(浮点型), Boolean(布尔型), Array(数组), Object(对象), NULL(空值). PHP ...

  10. Web 前端攻防(2014版)-baidu ux前端研发部

    http://fex.baidu.com/articles/page2/ Web 前端攻防(2014版) zjcqoo | 20 Jun 2014 禁止一切外链资源 外链会产生站外请求,因此可以被利用 ...