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. Mysql中与时间相关的统计分析

    最近项目需要统计一段日期范围内,根据每分钟.几分钟.每天分别统计汇总某些事件/指标的发生总次数,平均发生次数,因此总结了Mysql中与时间处理.统计相关的资料. 按分钟统计某一时间段内的数据 SELE ...

  2. pandas过滤包含特定字符串的行

    ~df.col3.str.contains('u|z')也就是在条件前面加~号,表示not

  3. Asp.Net中自以为是的Encode

    Asp.Net 引擎可能是不错,但是它把程序员想的太笨,会自以为是做很多自动的 Encode 和 Decode,以下文举例: 如果客户端我们 post 了如下的数据, 但是你实际得到的是: 也就是说, ...

  4. Installing Apache Spark on Ubuntu 16.04

    Santosh Srinivas on 07 Nov 2016, tagged onApache Spark, Analytics, Data Minin I've finally got to a ...

  5. OBjective-C:文件管理类NSFileManager

    文件管理类NSFileManager类:对文件进行创建.复制.重命名.删除等,一般不对文件内容进行操作. NSData类和NSMutableData类:相当于数据缓冲区  NSFileManager是 ...

  6. [8] 圆面(Round)图形的生成算法

    顶点数据的生成 bool YfBuildRoundVertices ( Yreal radius, Yreal height, Yuint slices, YeOriginPose originPos ...

  7. 第一章 Java常用的并发类

    注:本系列博客主要参考于<分布式Java应用:基础与实践>,林昊 著 1.常用的并发集合类 ConcurrentHashMap:线程安全的HashMap的实现 CopyOnWriteArr ...

  8. Python爬虫——使用 lxml 解析器爬取汽车之家二手车信息

    本次爬虫的目标是汽车之家的二手车销售信息,范围是全国,不过很可惜,汽车之家只显示100页信息,每页48条,也就是说最多只能够爬取4800条信息. 由于这次爬虫的主要目的是使用lxml解析器,所以在信息 ...

  9. scala 学习笔记七 基于类型的模式匹配

    1.介绍 Scala 提供了强大的模式匹配机制,应用也非常广泛. 一个模式匹配包含了一系列备选项,每个都开始于关键字 case.每个备选项都包含了一个模式及一到多个表达式.箭头符号 => 隔开了 ...

  10. Reinvent the Wheel Often

    Reinvent the Wheel Often Jason P. Sage Just use something that exists-it's silly to reinvent the whe ...