问题描述

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的更多相关文章

  1. [算法][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 ...

  2. 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 ...

  3. 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 ...

  4. LeetCode解题报告:Linked List Cycle && Linked List Cycle II

    LeetCode解题报告:Linked List Cycle && Linked List Cycle II 1题目 Linked List Cycle Given a linked ...

  5. 【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 ...

  6. [Linked List]Linked List Cycle,Linked List Cycle II

    一.Linked List Cycle Total Accepted: 85115 Total Submissions: 232388 Difficulty: Medium Given a linke ...

  7. 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 ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. ios应用数据存储方式

    一.ios应用常用的数据存储方式 1.plist(XML属性列表归档) 2.偏好设置 3.NSKeydeArchiver归档(存储自定义对象) 4.SQLite3(数据库,关系型数据库,不能直接存储对 ...

  2. iOS 获取IP地址

    一.获取本机IP地址 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 #import <ifadd ...

  3. 30道四则运算<2>单元测试

    该测试未实现除法 该测试中间多了/)两个符号,而且没有等号和回车. 该测试也没有符合除法要求 该测试也没有满足除法要求 该测试满足要求. 总结:程序中涉及到有除法的输出都有问题,多次改正未果:其他条件 ...

  4. 2016年11-29 mysql数据库

    php搭建环境:WAMP APPSERVERLAM架构:linux系统,apache服务器管理软件,mysql数据库,php语言 php apache.net iisjava tomcat mysql ...

  5. 四则运算app工程的进展

    深入分析五个四则运算app: 1.ALM-Addition 缺点:版本是英文版,不利于孩子们弄懂.  用选择题的方法来选择正确的答案,固然有运气答对的成分,不利于统计真实的水平. 优点:有图标统计答题 ...

  6. Android FM模块学习之二 FM搜索频道

    最近在学习FM模块,FM是一个值得学习的模块,可以从上层看到底层.上层就是FM的按扭操作和界面显示,从而调用到FM底层驱动来实现广播收听的功能. 看看FM启动流程:如下图: 先进入FMRadio.ja ...

  7. Pike学习笔记

    Pike的安装(Ubuntu环境) pike的语法非常像C++,但是它也是脚本语言,所以具有一般脚本语言的特性.一个简单的pike程序,hello world: int main() { write( ...

  8. 高手C++学习忠告~~[转载]

    1.把C++当成一门新的语言学习(和C没啥关系!真的.): 2.看<Thinking In C++>,不要看<C++变成死相>: 3.看<The C++ Programm ...

  9. js获取非行内样式

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  10. setSelection()和requestFocusFromTouch()

    昨天我遇到一个问题,点击返回的时候要在onResume()中用setSelection()定位到刚才点击的item,因为点击item进入后,我又一直点击“下一个”按钮,但是返回的时候listview不 ...