一、     题目

给定一个链表,假设链表中有环则返回环的開始节点,否则返回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. Salt Stack 官方文档翻译 - 一个想做dba的sa - 博客频道 - CSDN.NET

    OSNIT_百度百科 Salt Stack 官方文档翻译 - 一个想做dba的sa - 博客频道 - CSDN.NET Salt Stack 官方文档翻译 分类: 自动运维 2013-04-02 11 ...

  2. 【免费】iPhone上最好用的短信群发软件: 高速短信4.1

    免费的最新的联系人.群组.多人发送短信软件短信群发4.1已经出炉.欢迎下载! *归属地信息让你时时记着好友的地方,让陌生号码变得不陌生:  *您能够选择最经常使用的联系人然后发送高速短信; *群联系人 ...

  3. oracle列合并

    在很多场合,我们会须要用到oracle列合并,oracle提供了一些方法用于把某列的多行数据合并成一行. 一.10G曾经使用WMSYS.WM_CONCAT   wmsys.wm_concat将字段的值 ...

  4. AngularJS 学习笔记值post传值

    问题直接调用$http.post()方法时 传值格式是这样的 php接收端接收到的是json格式的,怎么做的跟Ajax post那样传值呢? 分析原因,angular的$http.post()方法默认 ...

  5. [置顶] 自己动手写Web容器之TomJetty之六:动态页面引入

    传送门 ☞ 1.Web服务内功经脉 传送门 ☞ 2.让服务动起来 传送门 ☞ 3.掀起请求盖头来 传送门 ☞ 4.静态页面起步 传送门 ☞ 5.包装请求参数 在上一节,我们已经完成了TomJetty服 ...

  6. 设定十分钟android在状态栏上集成的开源project推荐

    1.前言 于android kitkat 有一个新功能可以设置背景的手机状态栏,让手机风的整个界面格是一致的,它看起来非常酷,在今年的google i/o向上android l这样的风格.来如今看我们 ...

  7. 14.4.7 Configuring the Number of Background InnoDB IO Threads 配置 后台InnoDB IO Threads的数量

    14.4.7 Configuring the Number of Background InnoDB IO Threads 配置 后台InnoDB IO Threads的数量 InnoDB 使用bac ...

  8. cx_Oracle模块学习之绑定变量

    有些时候我们需要和程序交互,此时需要绑定量下面两个例子简介在SELECT 和 DML 里面绑定变量的用法 SELECT 里面的绑定变量 [root@Ora10G py]# cat SelectBind ...

  9. 13、Cocos2dx 3.0游戏开发找小三之3.0中的Director :郝萌主,一统江湖

    重开发人员的劳动成果.转载的时候请务必注明出处:http://blog.csdn.net/haomengzhu/article/details/27706967 游戏中的基本元素 在曾经文章中.我们具 ...

  10. 怎样用Java编写一段代码引发内存泄露

    通过下面步骤能够非常easy产生内存泄露(程序代码不能訪问到某些对象,可是它们仍然保存在内存中): 应用程序创建一个长时间执行的线程(或者使用线程池,会更快地发生内存泄露). 线程通过某个类载入器(能 ...