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. Solution Explorer 和 Source Control Explorer 的 View History 异同

    如果查看别人对代码的修改,你可能会非常烦恼与在 Solution Explorer 中看历史版本看不全,如下: 实际上,你想看到的是对于整个解决方案,全部的历史版本,那应该跑去 Source Cont ...

  2. NLP知识十大结构

    NLP知识十大结构 2.1形式语言与自动机 语言:按照一定规律构成的句子或者字符串的有限或者无限的集合. 描述语言的三种途径: 穷举法 文法(产生式系统)描述 自动机 自然语言不是人为设计而是自然进化 ...

  3. Git 学习(七)标签管理

    Git 学习(七)标签管理 发布版本时,通常会先在版本库中打一个标签,这样,就唯一确定了打标签时刻的版本.取出某个标签的版本,就是把那个打标签的时刻的历史版本取出来.所以,标签也是版本库的一个快照. ...

  4. 使用swipemenulistview实现列表的左右滑动

    今天从网上找到一个第三方控件swipemenulistview,封装好的一个控件,可以实现列表的左右滑动,模仿qq的列表效果 下载地址为:https://github.com/baoyongzhang ...

  5. Android -- 在ScrollView中嵌套ListView

    在做一个工程,这个工程的布局可以相当的复杂,最外面是ScrollView,在ScrollView里面有两个Listview,这下好了,布局出来了,放在机子上跑,卡得想死有木有,信息乱跑乱出现,表示非常 ...

  6. nodejs检查已安装模块

    命令行 npm ls --depth 0

  7. 解决Synergy的鼠标无法从服务器(server)机屏幕移动到客户机(client)屏幕的问题

    我在工作时使用一台Win 7笔记本和一台Ubuntu台式机,为了提升工作效率,我使用Synergy在两台机器间共享了笔记本的鼠标和键盘,即笔记本作为服务器,台式机作为客户机. 这样使用了大概一年多,但 ...

  8. 第十一节,命名空间namespace

    1,命名空间的定义 命名空间可以把不同的方法分散到不同的文件去实现,如果你会objective-C,他的作用和里面的类目有异曲同工之妙.当然了也有很多不同的地方,首先要明白的是,命名空间并不是一个类, ...

  9. wepy - 与原生有什么不同($pages,$interceptors)

    wepy内部封装的一些基类,我们要注意以 “$”开头命名,最好不用 关于wepy基类文档,请查看 关于$apply,其实就是主动刷新DOM,来更新数据. 何时使用它? 答. 你为data里面的数据进行 ...

  10. centos下两种方法安装git

    来自:http://blog.slogra.com/post-176.html 今天下个包需要使用git,网上找了下看到大多数只有编译安装,并且编译安装还有错,不知道他们也没有实验过,这里我来给大家介 ...