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 ...
随机推荐
- 10165 - Stone Game(Nim游戏)
UVA 10165 - Stone Game 题目链接 题意:给定n堆石子,每次能在一堆取1到多个.取到最后一个赢,问谁赢 思路:就裸的的Nim游戏,利用定理求解 代码: #include <s ...
- Craig可能是个冲浪爱好者
最近有个叫Dweeb的Mac管理员,在他的blog中声称发现,主管OS X和iOS等软件产品的苹果资深副总裁Craig是一个冲浪爱好者.他通过对6月10日的苹果WWDC发布会视频的研究,指出Craig ...
- 邮件应用Acompli和日历应用Sunrise(传微软曾考虑以80亿美元收购企业通讯公司Slack)
http://tech.163.com/16/0305/10/BHCU8EHO000915BD.html http://www.cnbeta.com/articles/480835.htm
- crontab linux
第1列分钟1-59第2列小时1-23(0表示子夜)第3列日1-31第4列月1-12第5列星期0-6(0表示星期天)第6列要运行的命令 下面是crontab的格式:分 时 日 月 星期 要运行的命令 这 ...
- 转]解析C语言中的sizeof
解析C语言中的sizeof 一.sizeof的概念 sizeof是C语言的一种单目操作符,如C语言的其他操作符++.--等.它并不是函数.sizeof操作符以字节形式给出 了其操作数的存储大小.操作数 ...
- Linux下select, poll和epoll IO模型的详解
http://blog.csdn.net/tianmohust/article/details/6677985 一).Epoll 介绍 Epoll 可是当前在 Linux 下开发大规模并发网络程序的热 ...
- callback用法简介
源地址:https://argcv.com/articles/2669.c callback,函数的回调,从ANSI C开始,一直被广为使用.无论是windows API的所谓消息机制,动态链接库的调 ...
- 与众不同 windows phone (20) - Device(设备)之位置服务(GPS 定位), FM 收音机, 麦克风, 震动器
原文:与众不同 windows phone (20) - Device(设备)之位置服务(GPS 定位), FM 收音机, 麦克风, 震动器 [索引页][源码下载] 与众不同 windows phon ...
- PHP+lghttpd + postgresql 环境搭建
PHP+lghttpd + postgresql 环境搭建 Linux 下PHP环境搭建 安装环境:PHP+lghttpd + postgresql 1:yum install lighttpd 红旗 ...
- cocos2d-x 3.0 使用最新物理引擎的一个源代码实例
1.碰撞函数參数由两个变成一个了 2.检測不到碰撞.须要设置那三个參数.同一时候还要设置成动态的. body进行设置. 3.初始入口文件也发生了改变. 附录上我近期调试好的cocos2d-x 3.1 ...