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. ODBC 小例

    #include "stdafx.h"#include <windows.h>#include <stdio.h>#include <iostream ...

  2. php 应用 cpu 100% 调试方法

    找出进程占用cpu高的原因. 进程占用cpu高,一般是由于进程长时间占用cpu,又没有主动释放占用.如果想主动释放cpu,可以调用sleep.在写程序的时候,尤其要注意while 等循环的地方. 找出 ...

  3. 用js创建XMLHttpRequest对象池[转]

    //使用literal语法定义一个对象:XMLHttp var XMLHttp = { //定义第一个属性,该属性用于缓存XMLHttpRequest对象的数组 XMLHttpRequestPool: ...

  4. word 转 PDF时报错

    利用微软自带的com组件,把word转化成Pdf,利用vs2012调试时没有问题,但是发布到IIS时出错,错误为: 检索 COM 类工厂中 CLSID 为 {} 的组件时失败,原因是出现以下错误: 8 ...

  5. 微信浏览器——User Agent

    在iPhone 返回 Mozilla/5.0 (iPhone; CPU iPhone OS 5_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gec ...

  6. Phonegap hello world 不容易啊~!

    今天一个项目要用phonegap,当初就是觉得phonegap配置太tmd的麻烦了,所以转头appcan,但今天项目必须用-- 先是看到官方说用nodejs装,tmd的,总是重复同一个错误,安装不起, ...

  7. Android设计画面中有EditText时取消启动时自动获得焦点调用系统输入法的方法

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools= ...

  8. bzoj 2440 简单莫比乌斯反演

    题目大意: 找第k个非平方数,平方数定义为一个数存在一个因子可以用某个数的平方来表示 这里首先需要考虑到二分才可以接下来做 二分去查找[1 , x]区间内非平方数的个数,后面就是简单的莫比乌斯反演了 ...

  9. “更高效率:标准化+简约风+移动化”--K2 BPM老客户交流会

    主题:工作流主数据标准化和移动工作流带来的企业沟通建设机会 嘉宾:李瑞延(盛大网络IT总监) 公司管理需要更好的工作流 -为决策提供依据 通过对各级业务公司各类流程数据的获取与分析,为管理决策提供必要 ...

  10. Web体系=资源+URI+表示

    概述 Web有三个核心概念:资源(Resource).URI(UniformResource Identifer,统一资源标识符).表示(Representation).一个资源由一个URI进行标识. ...