[LeetCode OJ] Linked List Cycle II—Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
/**
* 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 *p1, *p2; //p1和p2从链表的第一个节点出发,p1每次移动一个节点,p2每次移动两个节点,若两个指针重逢,则说明有环存在,否则若p2遇到NULL指针,说明无环存在
p1 = p2 = head; if(p2==NULL || p2->next==NULL)
return NULL;
p1 = p1->next;
p2 = p2->next->next; while(p1!=p2)
{
if(p2==NULL || p2->next==NULL)
return NULL;
p1 = p1->next;
p2 = p2->next->next; }
//此时p1==p2,说明有环存在。利用定理:p1和p2相遇处距离链表的第一个节点的长度是环长度的整数倍,来求环起始点。让一个指针从head处开始出发,另一个指针从相遇处出发,这两个指针必然在环起始节点处相遇
ListNode * CircleBegin_Node;
p1 = head;
while(p1!=p2)
{
p1 = p1->next;
p2 = p2->next;
}
CircleBegin_Node = p1;
return CircleBegin_Node;
}
};
[LeetCode OJ] Linked List Cycle II—Given a linked list, return the node where the cycle begins. If there is no cycle, return null.的更多相关文章
- 【LeetCode OJ】Palindrome Partitioning II
Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...
- 【LeetCode OJ】Path Sum II
Problem Link: http://oj.leetcode.com/problems/path-sum-ii/ The basic idea here is same to that of Pa ...
- 【LeetCode OJ】Word Ladder II
Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...
- 【LEETCODE OJ】Single Number II
Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...
- 【LeetCode OJ】Word Break II
Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...
- LeetCode OJ——Pascal's Triangle II
http://oj.leetcode.com/problems/pascals-triangle-ii/ 杨辉三角2,比杨辉三角要求的空间上限制Could you optimize your algo ...
- LeetCode OJ 45. Jump Game II
Given an array of non-negative integers, you are initially positioned at the first index of the arra ...
- LeetCode OJ 229. Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. The algorit ...
- LeetCode OJ 63. Unique Paths II
Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...
随机推荐
- jquery 插件JTable使用
http://www.jtable.org/ 下载后增加: Add these lines to the HEAD section of your HTML document: <!-- Inc ...
- sphinx插入css
使用role指令达到目的. We can put following lines at the beginning of our RST file to specify its style. .. r ...
- Java socket 说明 以及web 出现java.net.SocketException:(Connection reset或者Connectreset by peer:Socket write error)的解释
另外http://www.cnblogs.com/fengmk2/archive/2007/01/15/using-Socket.html可供参考 一Java socket 说明 所谓socket ...
- HDU 1523 Decoding Morse Sequences
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1523 此题大意为 给你一串摩尔斯密码 再给你一个字典(下面单词本) 用下面的单词组合成给你的摩尔斯密 ...
- (转载)教你在PHP中使用全局变量
(转载)http://www.chinaz.com/program/2009/0123/64261.shtml 即使开发一个新的大型PHP程序,你也不可避免的要使用到全局数据,因为有些数据是需要用到你 ...
- i++和++i的老问题
对于++j,该式是指先将j的值自加1,然后再取j的值.自增过后参与计算 i的值也为10:对于i++,该表达式是指先取i的值做运算,再将i加1.参见过计算后再自增
- server 2008 ftp 环境重点说明
最近 在弄ftp 环境,但是 到server 2008 r2 这个系统之后,按照之前的方法 不行了 具体情况如下 利用本机 资源管理器 访问不了,根本不出现 登录框 提示 然后 到ftp 站点 ...
- 2013=10=19 ENGLISH 翻译
数据结构习题及答案 严蔚敏_课后习题答案 http://www.doc88.com/p-243584884293.html 273089354 随着女性获得平等权力的趋势,女性日渐增长的经济权力以及为 ...
- Vagrant虚拟机的配置管理
Vagrant虚拟机的配置管理 一.shell配置管理 二.使用Puppet进行配置管理 三.案例 Apache服务器的自动配置 3.1 shell配置管理 3.2 puppet配置管理 ps:由于最 ...
- Mysql学生管理系统:表的建立,外键一对多,多对多关系,中间关联表的建立
学生管理系统 管理员注册/登录/注销 注册班级(班级详细信息) 注册学生信息 查看班级信息/查看老师资料 教师注册/注销 查看教师资料 查看学生资料 根据名称/班级/ 查看学生详细信息--支持模 ...