leetcode:linked_list_cycle_II
一、 题目
给定一个链表,假设链表中有环则返回环的開始节点,否则返回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的更多相关文章
- 我为什么要写LeetCode的博客?
# 增强学习成果 有一个研究成果,在学习中传授他人知识和讨论是最高效的做法,而看书则是最低效的做法(具体研究成果没找到地址).我写LeetCode博客主要目的是增强学习成果.当然,我也想出名,然而不知 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- [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 ...
- 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 ...
- Leetcode 笔记 112 - Path Sum
题目链接:Path Sum | LeetCode OJ Given a binary tree and a sum, determine if the tree has a root-to-leaf ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
随机推荐
- Windows 编程之 对话框总结
关于对话框 1 对话框种类 对话框也是一种资源,Windows中对话框分为模态的和非模态的,以及Windows系统中已经定义好的那些通用对话框,比方打开文件,目录,调色板,字符串查找等. 2 模态对话 ...
- Android菜鸟的成长笔记(4)——你真的理解了吗?
原文:Android菜鸟的成长笔记(4)--你真的理解了吗? 在上一篇中我们查看了QQ的apk源文件中的布局结构,并仿照QQ完成了我们第一个应用的界面,详细请看<Android菜鸟的成长笔记&g ...
- Java常用代码段 - 未完待续
记录一些自己写项目常用的代码段. 格式化常用日期格式 Date date = new Date(System.currentTimeMillis()); DateFormat d3 = DateFor ...
- Swift - 告警框(UIAlertView)的用法
1,下面代码创建并弹出一个告警框,并带有“取消”“确定”两个按钮 (注:自IOS8起,建议使用UIAlertController) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...
- 忽然想到:QProcess的Read功能太强,如果有什么搞不定的,可以调用外部程序 good
这样就可以用其它语言来无限扩展它的功能了,比如golang,比如Delphi
- Linux搭建Tomcat环境
安装Tomcat 1)下载apache-tomcat-7.0.42.tar.gz http://tomcat.apache.org/download-70.cgi 2)#tar -zxv ...
- Android面向HTTP协议发送get请求
/** * 採用get请求的方式 * * @param username * @param password * @return null表示求得的路径有问题,text返回请求得到的数据 */ pub ...
- LeetCode——Populating Next Right Pointers in Each Node II
Follow up for problem "Populating Next Right Pointers in Each Node". What if the given tre ...
- delphi 对抗任务管理器关闭(提升进程到Debug模式,然后设置进程信息SE_PROC_INFO)
[delphi] view plain copy program Project1; uses Windows; {$R *.res} function MakeMeCritical(Yes: Boo ...
- touch修改文件的修改时间和访问时间,ls --full-time显示文件详细,stat命令
1. 同时修改文件的修改时间和访问时间 touch -d "2010-05-31 08:10:30" test.doc 2. 只修改文件的修改时间 touch -m -d &quo ...