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. 线程池的corePoolSize、maximumPoolSize和poolSize

    什么是线程池: 为了避免系统频繁的创建和销毁线程,我们可以将创建的线程进行复用.在线程池中总有那么几个活跃的线程,也有一定的最大值限制,一个业务使用完线程之后,不是立即销毁而是将其放入到线程池中,从而 ...

  2. BULLET物理DEMO最新版本

    鼠标右键按下并拖动         旋转视角WSAD                         前后左右RF                             上下QE           ...

  3. [10] 圆管(Pipe)图形的生成算法

    顶点数据的生成 bool YfBuildPipeVertices ( Yreal radius, Yreal assistRadius, Yreal height, Yuint slices, YeO ...

  4. [Debug] Debug Node.js Application by using Chrome Dev tools

    For example you have a server.js file, and you want to debug some problems; What you can do is: node ...

  5. [Backbone]8. Level 7: Router and history

    1. Ceate a route Class var AppRouter = Backbone.Router.extend({ }); 2. Add a route name it "sho ...

  6. python使用md5处理下载图片

    import urllib2 import hashlib opener = urllib2.build_opener() req = opener.open("http://avatar. ...

  7. Linux c 管道文件-进程间的通信 mkfifo、pipe

    管道文件: 1.       创建管道mkfifo(命名管道) #include<sys/stat.h> int mkfifo( const  char  *pathname, mode_ ...

  8. PHP 表单 - 4(验证邮件和URL)

    PHP 表单 - 验证邮件和URL 本章节我们将介绍如何验证 names(名称), e-mails(邮件), 和 URLs. PHP - 验证名称 以下代码将通过简单的方式来检测 name 字段是否包 ...

  9. taro alias 的使用

    用来配置目录别名,从而方便书写代码引用路径.例如,使用相对路径书写文件引用如下: import A from '../../componnets/A' import Utils from '../.. ...

  10. sublime 格式化XML文件

    下载插件 下载下来后将其解压到D:\tools\Sublime Text 3109\Data\Packages 打开sublime,preferences->browse packages,会弹 ...