Q3: 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?
解决原理
以不同的速度去遍历链表,slow指针速度是每步一个结点,fast指针速度是每步两个结点
slow遍历节点数是m,fast遍历节点数是2m
假设链表带环,则slow指针与fast指针一定相遇,因为两指针的相对速度是每步一个节点
假设环的长度是L,则当相遇时,fast遍历的节点数比slow遍历的节点数多NL个,N为正整数
2m = m + NL ——> m = NL,m是环长度的整数倍
相遇时的节点位置与起始点相距m个节点,即相距环长度的整数倍
这样如果令slow指针指向链表起始点,fast指针仍然指向相遇点,并且让slow指针与fast指针以相同的速度遍历链表
则当slow指针指向环的起始点时,因为fast与slow的相对距离是NL,则此时fast必定也指向环的起始位置
所以,当两指针指向同一节点时,此节点即为环的起始点
代码C++
/**
* 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(){
if(!slow)
return NULL;
else
slow = slow->next;
if(!fast->next || !fast->next->next)
return NULL;
else
fast = fast->next->next;
if(slow == fast){
slow = head;
while(slow != fast){
slow = slow -> next;
fast = fast -> next;
}
return slow;
}
}
}
};
未改进
Q3: 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 ...
- 15. 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 solve i ...
- Java for LeetCode 142 Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- LeetCode解题报告:Linked List Cycle && Linked List Cycle II
LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...
- 【LeetCode练习题】Linked List Cycle II
Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you solve it ...
- [Linked List]Linked List Cycle,Linked List Cycle II
一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...
- Linked List Cycle && Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Note ...
- 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: Ca ...
- 141. Linked List Cycle&142. Linked List Cycle II(剑指Offer-链表中环的入口节点)
题目: 141.Given a linked list, determine if it has a cycle in it. 142.Given a linked list, return the ...
随机推荐
- 在windows上搭建ftp服务
在控制面板->程序和功能->打开或关闭Windows功能中开启ftp和IIS信息服务管理器 在控制面板->管理工具中打开Internet信息服务管理器->添加ftp站点 建好之 ...
- Cocos2d-x 2.x项目创建
cocos2d-x下载地址:http://www.cocos2d-x.org/download 2.0之后的创建项目 1. cd cocos2d-x-2.2.1/tools/project-creat ...
- ASP.NET获取客户端及服务器的信息
客户端信息: 1. 在ASP.NET中专用属性: 获取服务器电脑名:Page.Server.ManchineName 获取用户信息:Page.User 获取客户端电脑名:Page.Request.Us ...
- C# 控件不刷新问题
/********************************************************************** * C# 控件不刷新问题 * 说明: * 当网络连接出问 ...
- dedecms 列表页调用自定义字段
在列表附加字段中添加自己定义的字段 如: lwulr调用:{dede:list addfields="lwurl" channelid="1"}[field:l ...
- Prepared Java infrastructure for distributed scenarios
code is sited on: https://github.com/zhoujiagen/javaiospike progress 2015/5/27 Nio/Nio2 examples, us ...
- Java 存储过程调用
//配置文件 private static ClientServiceConfigUtil configUtil = new ClientServiceConfigUtil("/Databa ...
- OpenCV图像Surf与flann特征点(转载)
Surf(Speed Up Robust Feature) Surf算法的原理 ...
- 66. Plus One
Given a non-negative number represented as an array of digits, plus one to the number. The digits ar ...
- C++ Primer : 第十二章 : 文本查询程序
C++ Primer书上这个例子讲的很不错,写写帮助自己理解标准库和智能指针. .h 文件内容 #include <fstream> #include <iostream> # ...