LeetCode之“链表”:Linked List Cycle && Linked List Cycle II
1.Linked List Cycle
题目要求:
Given a linked list, determine if it has a cycle in it.
Follow up:
Can you solve it without using extra space?
刚看到这道题,很容易写出下边的程序:
bool hasCycle(ListNode *head) {
ListNode *a = head, *b = head;
while(a)
{
b = a->next;
while(b)
{
if(a == b)
return true;
b = b->next;
}
a = a->next;
}
return false;
}
这个程序最大的问题在于“死循环”,例如在下边这种情况就会进入死循环:

要解决这个问题,我们可以定义两个指针slow、fast,slow每次前进一步,fast每次前进两步。如果链表存在环,则在循环一定次数后,slow与fast一定会重合(找个例子推导下就明白了)。具体从程序如下:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
bool hasCycle(ListNode *head) {
if(head == nullptr || head->next == nullptr)
return false; ListNode *slow = head, *fast = head;
while(fast != nullptr && fast->next != nullptr)
{
slow = slow->next;
fast = fast->next->next;
if(slow == fast)
return true;
} return false;
}
};
2.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?
这道题难度还是挺大的。具体解法参考自一博文:
首先看图:

从链表起始处到环入口长度为:a,从环入口到Faster和Slower相遇点长度为:x,整个环长为:c。
假设从开始到相遇,Slower走过的路程长为s,由于Faster的步速是Slower的2倍,那么Faster在这段时间走的路程长为2s。
而对于Faster来说,他走的路程还等于之前绕整个环跑的n圈的路程nc,加上最后这一次遇见Slower的路程s。
所以我们有:
2s = nc + s
对于Slower来说,他走的路程长度s还等于他从链表起始处到相遇点的距离,所以有:
s = a + x
通过以上两个式子代入化简有:
a + x = nc
a = nc - x
a = (n-1)c + c-x
a = kc + (c-x)
那么可以看出,c-x,就是从相遇点继续走回到环入口的距离。上面整个式子可以看出,如果此时有个pointer1从起始点出发并且同时还有个pointer2从相遇点出发继续往前走(都只迈一步),那么绕过k圈以后, pointer2会和pointer1在环入口相遇。这样,换入口就找到了。
具体程序如下:
/**
* 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* slow = head;
ListNode* fast = head;
while (true){
if(!fast || !fast->next)
return nullptr; slow = slow->next;
fast = fast->next->next;
if (slow == fast)
break;
} slow = head;
while (slow != fast){
slow = slow->next;
fast = fast->next;
}
return slow;
}
};
LeetCode之“链表”:Linked List Cycle && Linked List Cycle II的更多相关文章
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
题目要求 Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up: Can you so ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- LeetCode 430. Faltten a Multilevel Doubly Linked List
题目链接:LeetCode 430. Faltten a Multilevel Doubly Linked List class Node { public: int val = NULL; Node ...
- LeetCode 单链表专题 (一)
目录 LeetCode 单链表专题 <c++> \([2]\) Add Two Numbers \([92]\) Reverse Linked List II \([86]\) Parti ...
- leetcode 单链表相关题目汇总
leetcode-19-Remove Nth From End of List—移除链表中倒数第n个元素 leetcode-21-Merge Two Sorted Lists—两个已排序链表归并 ...
- Leetcode解题-链表(2.2.0)基础类
1 基类的作用 在开始练习LeetCode链表部分的习题之前,首先创建好一个Solution基类,其作用就是: Ø 规定好每个子Solution都要实现纯虚函数test做测试: Ø 提供了List ...
- 【算法题 14 LeetCode 147 链表的插入排序】
算法题 14 LeetCode 147 链表的插入排序: 解题代码: # Definition for singly-linked list. # class ListNode(object): # ...
- LeetCode 80. 删除排序数组中的重复项 II
LeetCode 80. 删除排序数组中的重复项 II
- Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II)
Leetcode之二分法专题-240. 搜索二维矩阵 II(Search a 2D Matrix II) 编写一个高效的算法来搜索 m x n 矩阵 matrix 中的一个目标值 target.该矩阵 ...
- LeetCode:137. 只出现一次的数字 II
LeetCode:137. 只出现一次的数字 II 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现了三次.找出那个只出现了一次的元素. 说明: 你的算法应该具有线性时间复杂度. ...
随机推荐
- 前端CSS技术全解(一)
一.概述 1)用HTML完成样式工作 哪个标签有哪个属性难以记忆 需求变更影响较大(例如像修改成功法则以下的文字颜色需要修改4个地方) <h1 align="center"& ...
- 查看oracle数据库的连接数以及用户 (转:http://blog.itpub.net/24558279/viewspace-752293/)
select2.查询oracle的并发连接数4select3.查看不同用户的连接数6select4.查看所有用户:8select5.查看用户或角色系统权限(直接赋值给用户或角色的系统权限):10sel ...
- time,gettimeofday,clock_gettime
time()提供了秒级的精确度 1.头文件 <time.h> 2.函数原型 time_t time(time_t * timer) 函数返回从UTC1970-1-1 0:0:0开始到现在的 ...
- Swift变量名的一种玩法
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 是的,Swift的变量名可以用任何合法的Unicode字符,这 ...
- C控制台实现模拟平抛运动算法
平抛运动这个相信读了高中物理都知道这个概念了,详细的我就不说了,不明白的看看百度: 平抛运动 接下来看看用控制台实现的平抛运动算法: #include <stdio.h> #include ...
- JQuery之DOM操作及常用函数
属性操作 attr(name)获取属性值 var imgSrc = $("img").attr("src") attr(name,value)设置属性值 $(& ...
- Android项目开发填坑记-Fragment的onAttach
背景 现在Android开发多使用一个Activity管理多个Fragment进行开发,不免需要两者相互传递数据,一般是给Fragment添加回调接口,让Activity继承并实现. 回调接口一般都写 ...
- leetcode 3 Longest Substring Without Repeating Characters最长无重复子串
Given a string, find the length of the longest substring without repeating characters. For example, ...
- Android开发学习之路--NDK、JNI之初体验
好久没有更新博客了,最近一直在看一个仿微信项目,然后看源码并自己实现下,相信经过这个项目可以让自己了解一个项目中的代码以及种种需要注意的事项.不知不觉中博客已经快要40w访问量,而且排名也即将突破30 ...
- picasso图片缓存框架
picasso是Square公司开源的一个Android图形缓存库,地址http://square.github.io/picasso/,可以实现图片下载和缓存功能. picasso使用简单,如下 [ ...