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?

Hide Tags

Linked List Two Pointers

 
 

   开始犯二了一点,使用了node 的val 作为判断,其实是根据内存判断。找出链表环的起始位置,这个画一下慢慢找下规律便可以:
思路:
  1. 使用快慢指针,慢指针每次前进一步,快指针每次两步
  2. 如果快慢指针相遇了,那么将快指针从标记带链表头,改为每次前进一步
  3. 当快慢指针再次相遇便是环起始位置。

这样的实现,时间很快O(n),而且空间O(1)

#include <iostream>
using namespace std;
/**
* 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) {
if(head==NULL) return NULL;
ListNode * fast=head,*slow=head;
while(){
if(fast->next!=NULL) fast=fast->next;
else return NULL;
if(fast->next!=NULL) fast=fast->next;
else return NULL;
slow=slow->next;
if(fast==slow) break;
}
fast=head;
while(){
if(fast==slow) return slow;
fast=fast->next;
slow=slow->next;
}
return NULL;
}
}; int main()
{
ListNode node1(),node2(),node3(),node4(),node5();
node1.next=&node2;
node2.next=&node3;
node3.next=&node4;
node4.next=&node5;
node5.next=&node1;
Solution sol;
ListNode *ret = sol.detectCycle(&node1);
if(ret==NULL) cout<<"NULL"<<endl;
else cout<<ret->val<<endl;
return ;
}

[LeetCode] Linked List Cycle II 链表环起始位置的更多相关文章

  1. LeetCode Linked List Cycle 单链表环

    题意:给一个单链表,判断其是否出现环! 思路:搞两个指针,每次,一个走两步,另一个走一步.若有环,他们会相遇,若无环,走两步的指针必定会先遇到NULL. /** * Definition for si ...

  2. [LeetCode] Linked List Cycle 单链表中的环

    Given a linked list, determine if it has a cycle in it. Follow up: Can you solve it without using ex ...

  3. LeetCode Linked List Cycle II 和I 通用算法和优化算法

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

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

    Linked List Cycle II Given a linked list, return the node where the cycle begins. If there is no cyc ...

  5. [LeetCode] Linked List Cycle II 单链表中的环之二

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  6. [LeetCode] 142. Linked List Cycle II 链表中的环 II

    Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...

  7. [Leetcode] Linked list cycle ii 判断链表是否有环

    Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...

  8. [LeetCode] Linked List Cycle II, Solution

    Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...

  9. LeetCode Linked List Cycle II 单链表环2 (找循环起点)

    题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...

随机推荐

  1. wamp mysql服务意外停止

    出现问题: MySQL启动一段时间之后,意外停止.可以再次启动,但是过不了多久又自动停止了. 发现问题: 查看错误日志,发现以下问题: 解决方案: 网上网友分享以下操作: 1.删除data文件夹里面的 ...

  2. JZ2440开发板与ubuntu互ping,然后进行文件的共享和挂载

    操作手册如下:但本人直接用网线直接连通开发板的网口与电脑的网口没有成功过.采用路由器可以直接ping通,具体操作如下: 首先用网线将开发板和路由器连接.电脑无论是用wifi还是网线均可.然后关闭Win ...

  3. pandas知识点(数据结构)

    1.Series 生成一维数组,左边索引,右边值: In [3]: obj = Series([1,2,3,4,5]) In [4]: obj Out[4]: 0 1 1 2 2 3 3 4 4 5 ...

  4. 指向class的指针使用方法实例

    // pointer to classes example #include <iostream> using namespace std; class Rectangle { int w ...

  5. mac terminal基本命令

    文件目录 首先要清楚几个文件目录: " / "  :根目录 " ~ " :用户主目录的缩写.例如当前用户为esther,那么" ~ "展开来 ...

  6. angular用$sce服务来过滤HTML标签

    angular js的强大之处之一就是他的数据双向绑定这一牛B功能,我们会常常用到的两个东西就是ng-bind和针对form的ng-model.但在我们的项目当中会遇到这样的情况,后台返回的数据中带有 ...

  7. Jquery Dialog 详解(正在学习jquery,详解转载)

    文章来源:http://xufish.blogbus.com/logs/39583154.html AUTHOR:Jevoly 还是先看例子吧.另外如果要拖动.改变dialog的大小的话要加上ui.d ...

  8. Python框架之Django学习笔记(十四)

    Django站点管理(续·完) 本想昨天更新的,谁曾想昨天竟然是工作日!我就不吐槽昨天加班到十一点多了,需求增加无疑让我等蛋疼不已,忽而想起一首打油诗: 明月几时有,把酒问群友.不知这次版本,今晚能出 ...

  9. linux环境搭建系列之tomcat安装步骤

    前提: Linux centOS 64位 JDK 1.7 安装包从官网上下载 安装Tomcat之前要先安装JDK. 我的JDK是1.7版本的,所以Tomcat版本也选了7的 1.新建目录tomcat ...

  10. 利用js阻止表单提交

    (1) return false <form name="loginForm" action="login.aspx" method="post ...