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?

思路

这题是Linked List Cycle的进阶版

Given a linked list, determine if it has a cycle in it.

 bool hasCycle(ListNode *head) {
if(head == NULL) return false; //带环链表还要考虑只有单个元素的情况
ListNode *faster = head, *slower = head;
while(faster->next != NULL && faster->next->next != NULL && slower->next != NULL){//直接判断faster->next->next != NULL会抛错
faster = faster->next->next;
slower = slower->next;
if(faster == slower)
return true;
}
return false;
}

faster和slower相遇之后必然在环上,让slower再走一圈计算环的长度len。另让两个指针p1,p2从head开始走,p1比p2先走len步,这样当p2走到环开始处时,正好p1与p2第一次相遇。

 ListNode *detectCycle(ListNode *head) {
if(head == NULL) return NULL;
//带环链表要考虑只有单个元素的情况
ListNode *faster = head, *slower = head;
while(faster->next != NULL && faster->next->next != NULL && slower->next != NULL){//直接判断faster->next->next != NULL会抛错
faster = faster->next->next;
slower = slower->next;
if(faster == slower){
ListNode *p1 = head;//另让两个指针p1,p2从head开始走
ListNode *p2 = head;
slower = slower->next;//让slower再走一圈计算环的长度len
p1 = p1->next;//设len是环的长度,p1比p2先走len步
if(faster == slower) return faster;//自环
while(faster != slower){
slower = slower->next;
p1 = p1->next;
}
while(p1 != p2){//当p1与p2第一次相遇时,正好p2走到环开始处
p1 = p1->next;
p2 = p2->next;
}
return p2;
}
}
return NULL;
}

【题解】【链表】【Leetcode】Linked List Cycle II的更多相关文章

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

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

  3. [LeetCode] 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 ii 判断链表是否有环

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

  5. [LeetCode] Linked List Cycle II, Solution

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

  6. [LeetCode] Linked List Cycle 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 单链表环2 (找循环起点)

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

  8. [LeetCode]Linked List Cycle II解法学习

    问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...

  9. LeetCode——Linked List Cycle II

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

  10. Leetcode Linked List Cycle II

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

随机推荐

  1. 生成guid

    http://jingyan.baidu.com/article/84b4f565eebb9d60f6da3293.html

  2. [转]无IDE时编译和运行Java

    本文由 ImportNew - Grey 翻译自 dzone.欢迎加入Java小组.转载请参见文章末尾的要求. 最近 Java subreddit 出现了一篇”在没有IDE的情况下编译Java包” 的 ...

  3. VS构建工具介绍

    VS构建工具介绍 我们都知道C/C++源代码要生成可执行的.exe程序,需要经过编译.链接的过程.你在VS工具中只需要选择菜单Build或按一下F5可以编译.链接.运行了,其实IDE帮我隐藏了好多的具 ...

  4. CSS3学习教程:Media Queries详解

    说起CSS3的新特性,就不得不提到 Media Queries . Media Queries 的引入,其作用就是允许添加表达式用以确定媒体的情况,以此来应用不同的样式表.换句话说,其允许我们在不改变 ...

  5. linux常用的重要的命令: netstat

    Netstat 命令用于显示各种网络相关信息,如网络连接,路由表,接口状态 (Interface Statistics),masquerade 连接,多播成员 (Multicast Membershi ...

  6. PHP后台

    一.ajax提交表单 先引入ajax.js function ajax(url, fnSucc, fnFaild) { //1.创建Ajax对象 var oAjax=null; if(window.X ...

  7. Servlet作业--实现注册和登录

    1.注册页面  zhuce.html <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" " ...

  8. 5.2使用select,poll

    5.2 使用select,poll   // CPU占用率低,适用于很多简单场合 参考:UNIX环境高级编程 I/O多路转接 监测多个文件,只要有某一个文件可读/可写/异常或超时,即返回 int se ...

  9. IT公司100题-3-求数组的最大子序列的和

    问题描述: 输入一个整形数组,数组里有正数也有负数. 数组中连续的一个或多个整数组成一个子数组,每个子数组都有一个和. 求所有子数组的和的最大值.要求时间复杂度为O(n). 例如输入的数组为1, -2 ...

  10. java枚举类

    enum关键字用于定义枚举类,若枚举只有一个成员, 则可以作为一种单例模式的实现方式.   枚举类对象的属性不应允许被改动, 所以应该使用 private final 修饰. 枚举类的使用 priva ...