一、     题目

给定一个链表,假设链表中有环则返回环的開始节点,否则返回NULL。要求不用额外的空间完毕。

二、     分析

在I中,我们推断环的存在,即用slow和fast两个指针,设定步长fast=2;slow=1;假设两个指针能够相遇则环存在,此时假设二者相遇我们仅仅需将slow=head;同一时候设置两者步长都为1,则两者再次相遇的节点即为环的開始节点。

推导过程:(图烂勿吐)

当两者第一次相遇时,

slow走过S1=x + y;

fast走过S2=x + y + z + y,

又知S2=2 * S1;

所以,2 *(x+y) = x + y + z + y;

故,x = z;

<span style="font-size:18px;">/**
* 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(fast!=NULL&&fast->next != NULL) {
slow = slow->next;
fast = fast->next->next;
if(slow==fast) break;
}
if(fast==NULL||fast->next==NULL) {
return NULL;
}
slow=head;
while(slow!=fast) {
slow = slow->next;
fast = fast->next;
}
return fast;
}
};</span>

leetcode:linked_list_cycle_II的更多相关文章

  1. 我为什么要写LeetCode的博客?

    # 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...

  2. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  3. [LeetCode] Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  4. Leetcode 笔记 113 - Path Sum II

    题目链接:Path Sum II | LeetCode OJ Given a binary tree and a sum, find all root-to-leaf paths where each ...

  5. Leetcode 笔记 112 - Path Sum

    题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...

  6. Leetcode 笔记 110 - Balanced Binary Tree

    题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...

  7. Leetcode 笔记 100 - Same Tree

    题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...

  8. Leetcode 笔记 99 - Recover Binary Search Tree

    题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...

  9. Leetcode 笔记 98 - Validate Binary Search Tree

    题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...

随机推荐

  1. Passenger/Nginx/Debian快速部署Rails

    安装所需的linux包 sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl gi ...

  2. Android:自定义滚动边缘(EdgeEffect)效果

    Android可滚动控件(GridView.ListView.ScrollView等)当用户滚动到头的时候会有个边缘反馈效果,在4.0上默认为Holo蓝色效果.  如果您的App自定义了主题颜色,比如 ...

  3. Swift - 文本输入框(UITextField)的用法

    1,文本框的创建,有如下几个样式: UITextBorderStyle.None:无边框 UITextBorderStyle.Line:直线边框 UITextBorderStyle.RoundedRe ...

  4. BCB/Delphi中常用的VCL函数说明(字符串函数)

    本文档是ccrun(老妖)根据网上资料整理而成. --------------------内存分配--------------------函数名称:AllocMem函数说明:在队中分配指定字节的内存块 ...

  5. Android应用开发学习笔记之绘图

    作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 一.绘图常用类介绍 在Android中绘图时,常用到的几个类是Paint.Canvas.Bitmap和Bitmapt ...

  6. uu云验证码识别平台,验证码,验证码识别,全自动验证码识别技术,优优云全自动打码,代答题系统,优优云远程打码平台,uu云打码

    uu云验证码识别平台,验证码,验证码识别,全自动验证码识别技术,优优云全自动打码,代答题系统,优优云远程打码平台,uu云打码 优优云验证码识别答题平台介绍 优优云|UU云(中国公司)是全球唯一领先的智 ...

  7. vc笔记六

    通知消息(Notification message)是指这样一种消息,一个窗口内的子控件发生了一些 事情,需要通 知父窗口.通知消息只适用于标准的窗口控件如按钮.列表框.组合框.编辑框,以及 Wind ...

  8. 玩转Windows服务系列——Windows服务小技巧

    原文:玩转Windows服务系列——Windows服务小技巧 伴随着研究Windows服务,逐渐掌握了一些小技巧,现在与大家分享一下. 将Windows服务转变为控制台程序 由于默认的Windows服 ...

  9. 关于智普 - 千人免费学|Python培训|国内最权威python培训|html5

    关于智普 - 千人免费学|Python培训|国内最权威python培训|html5 智普教育隶属于北京顶嵌开源科技有限公司,成立于2008年. 智普开源是基于Linux系统的互联网开源学习平台,讲求务 ...

  10. 在WIN7系统的笔记本上建立WIFI热点

        成功与否的关键在于你是否依照顺序运行:       前置条件:右键"我的电脑"--"属性"--"设备管理器"--"网络适配 ...