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. <%%>用法初步认识

    <%%>是用于向客户端插入服务器代码所使用的一种标记 例如为了在HTML页面上展示由服务器提供的当前用户的某条信息或名字等便可使用 前台 <a href="home.asp ...

  2. php扩展开发-变量

    我们在php中用到的变量,在底层的C语言代码里是一个结构体,由四个成员组成typedef struct _zval_struct { zvalue_value value; /* 变量的值,也是一个结 ...

  3. list,tuple,set,dict汇总

      有序/无序 追加/删除元素 元素可/不可重复 元素类型 创建方式 List 有序 可追加删除追加:list.append(item),list.insert(index,item)删除:list. ...

  4. pip3 的安装 同时安装lxml和pygame

    ubuntu18.04中 首先查看自己电脑的python版本,一般都会有2, 和3 python -V python3 -V 查看pip版本 pip -V pip3 -V 现在我们就可以开始安装我们的 ...

  5. Codeforces Round #464 (Div. 2) D. Love Rescue

    D. Love Rescue time limit per test2 seconds memory limit per test256 megabytes Problem Description V ...

  6. 遗传算法 | C++版GA_TSP

    嗯哼,时隔半年,再次有时间整理关于组合优化问题——旅行商问题(Traveling Salesman Problem, TSP),这次采用的是经典遗传算法(Genetic Algorithm, GA)进 ...

  7. Python 有序字典简介

    Table of Contents 1. 有序字典-OrderedDict简介 1.1. 示例 1.2. 相等性 1.3. 注意 2. 参考资料 有序字典-OrderedDict简介 示例 有序字典和 ...

  8. Unity 与Mono和.Net的关系

    一.分析 首先,我们要知道Unity,Mono,.Net 三者的关系.需要简单说一下.Net. .Net拥有跨语言,跨平台性. 跨语言:就是只要是面向.Net平台的编程语言,用其中一种语言编写的类型就 ...

  9. oracle 11g 版本自带移除,省时省力

    ---oracle删除 app\Administrator\product\11.2.0\dbhome_1\deinstall.bat 指定要取消配置的所有单实例监听程序 [LISTENER]: En ...

  10. Jquery 实现层的拖动,支持回调函数

    最近在写一个CMS内容管理系统,前台基本是用ajax异步请求服务器,通过ashx处理,返回json格式处理.由于需要更加人性化的界面,所以采用到了拖动层的操作. 以下是拖动层的主要核心方法,本来想写成 ...