Leetcode Linked List Cycle II
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?
借用博客http://www.cnblogs.com/hiddenfox/p/3408931.html的图

设环的距离为L = (b+c),无环的距离为a,
假设在时间t相遇,慢指针行驶距离为x,则 1 x t = x,即t=x
则快指针行驶的距离为2t = 2x,
则慢指针环上停留点为(x-a)%L,快指针停留点为 (2x-a)%L,由于在t时刻相遇,故(x-a)%L = (2x-a)%L,
根据同余定理 x%L = 0,
当快指针相遇后减慢速度为1,快指针从z继续行走a长度停下,则行走的路程为2x+a,在环上的位置为(2x+a-a)%L = 2x%L=2(x%L) = 0,即回到环的起始点,故知道环的起始点
ListNode* hasCycle(ListNode* head){
if(head == NULL || head->next == NULL) return false;
ListNode* first = head, *second = head;
while(second!=NULL && second->next!=NULL){
first = first->next;
second = second->next->next;
if(first == second) return first;
}
return NULL;
}
ListNode *detectCycle(ListNode *head){
ListNode* cycleNode = hasCycle(head);
if(cycleNode != NULL){
ListNode* startNode = head;
while(startNode!=cycleNode){
cycleNode = cycleNode->next;
startNode = startNode->next;
}
}
return cycleNode;
}
Leetcode Linked List Cycle II的更多相关文章
- 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 ...
- 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 ...
- [LeetCode] Linked List Cycle II 单链表中的环之二
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [Leetcode] Linked list cycle ii 判断链表是否有环
Given a linked list, return the node where the cycle begins. If there is no cycle, returnnull. Follo ...
- [LeetCode] Linked List Cycle II, Solution
Question : Given a linked list, return the node where the cycle begins. If there is no cycle, return ...
- [LeetCode]Linked List Cycle II解法学习
问题描述如下: Given a linked list, return the node where the cycle begins. If there is no cycle, return nu ...
- LeetCode——Linked List Cycle II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- [LeetCode] Linked List Cycle II 链表环起始位置
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- LeetCode Linked List Cycle II 单链表环2 (找循环起点)
题意:给一个单链表,若其有环,返回环的开始处指针,若无环返回NULL. 思路: (1)依然用两个指针的追赶来判断是否有环.在确定有环了之后,指针1跑的路程是指针2的一半,而且他们曾经跑过一段重叠的路( ...
随机推荐
- js冒泡排序与二分法查找
冒泡排序 var attr=[1,5,7,6,3,9,2,8,4]; var zj=0; //控制比较轮数 for(var i=0;i<attr.length-1;i++) { //控制每轮的比 ...
- 以 MAMP 为 Mac OS X 安装并设置 PHP开发环境
PHP 页需要通过 Web 服务器处理.因此,要在 Dreamweaver 中使用 PHP 进行开发,您需要访问支持 PHP 的 Web 服务器和 MySQL 数据库.phpMyAdmin 也很实用, ...
- tomcat安装服务和内存参数设置
第一:安装服务 在dos窗口进入到tomcat的bin目录下,通过如下命令即可将tomcat安装成服务 service.bat install Tomcat2 其中Tomcat2是服务的名称 如果启动 ...
- 扩展LV
LVM最大的特性就是可以弹性调整磁盘容量下面扩展一个已经存在的LV [root@ol6--rac1 mnt]# lvdisplay --- Logical volume --- LV Path /de ...
- HDU5781 ATM Mechine(DP 期望)
应该是machine 和POJ3783 Balls类型相似. 现在上界为i元,猜错次数最多为j时,开始猜测为k元,有两种情况: 1 猜中:(i - k + 1) * dp[i - k][j] 2 猜不 ...
- <转>ORA-12154: TNS: 无法解析指定的连接标识符
相信作为ORACLE数据库的开发人员没有少碰到“ORA-12154: TNS: 无法解析指定的连接标识符”,今天我也又碰到了类似的情况,将我的解决方法进行小结,希望能对碰到同样问题的友人们提供帮助. ...
- 【leetcode】Candy
题目描述: There are N children standing in a line. Each child is assigned a rating value. You are giving ...
- 去除字符串中的html标记及标记中的内容
去除字符串中的html标记及标记中的内容 --1.创建函数 create function [dbo].[clearhtml] (@maco varchar(8000)) returns varcha ...
- ettercap局域网内DNS欺骗(隔壁的哥们轻一点 ...)
转自:http://www.cnblogs.com/hkleak/p/5043063.html 笔记一:ettercap是什么? 我们在对WEB安全检测的时候都会用到Cain和netfuke这两款工具 ...
- Liferay 6.2 改造系列之十九:修改站点设置的表单内容
在/portal-master/portal-impl/src/portal.properties文件中,有如下配置: # # Input a list of sections that will b ...