[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 ...
随机推荐
- 【HDOJ】3220 Alice’s Cube
状态压缩+逆向BFS.方向数组就是任意相邻的两点(初始化时减1),每个顶点均有4个相邻点.因此,共有16*4/2=32个方向.按序排列即可找到. /* 3220 */ #include <ios ...
- 从ulimit命令看socket的限制
从ulimit命令看socket的限制 在Linux下面部署应用的时候,有时候会遇上Socket/File: Can’t open so many files的问题,比如还有Squid做代理,当文 ...
- 账户管理groupadd groupmod groupdel usermod usermod userdel
http://www.cnblogs.com/ggjucheng/archive/2012/08/21/2648380.html http://blog.csdn.net/qq1603013767/a ...
- phpMyAdmin 完整路径泄露漏洞2
漏洞名称: phpMyAdmin 完整路径泄露漏洞 CNNVD编号: CNNVD-201307-651 发布时间: 2013-08-09 更新时间: 2013-08-09 危害等级: 中危 漏洞类 ...
- jquery 学习第一课之start
1.$选取符 ( $ == jQuery ) (1) $("div").addClass("special");选取本页面中的所有<div>元素,然 ...
- Visual Studio Code尝试体验
背景了解 偶然间看到一篇大赞Visual Studio Code的文章,就搜索了一下,发现网上基本一致的好评.虽然微软在2015年4月29号 Build 2015 大会上才发布,但免费,轻量,跨平台版 ...
- HDU-2087 剪花布条
http://acm.hdu.edu.cn/showproblem.php?pid=2087 剪花布条 Time Limit: 1000/1000 MS (Java/Others) Memory ...
- poj3258
题目翻译 二分法(其实两个单词的意思分别是河,跳格子游戏,至于为啥翻译成二分法- -只能说英语博大精深啊) 奶牛每年举办一场有特色的跳格子游戏(很明显题目翻译错误)涉及到在河里从一块岩石跳到另一块岩石 ...
- 关闭ES动态创建mapping
使用ES的默认配置会使我们在索引不存在于mapping中的字段时,会自动创建. 这无疑会给我们带来困扰. 在我们不想要某个字段被搜索的时候,我们可以在开始关闭动态创建mapping. 执行如下操作: ...
- Solr系列二:Solr与mmseg4j的整合
mmseg4j是一个很好的中文分词器,solr与mmseg4j的整合也非常简单.如下: 第一步:下载mmseg4j的jar包,网上搜索一下有很多下载地址,如下是csdn上的一个连接:http://do ...