[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 ...
随机推荐
- 浅度围观SBJson
JSON JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度, 那么,JSON到底是什么? JSON就是一串字符串 只不 ...
- 2015第37周一struts2 jstl 标签
1.在jstl中使用struts2 <c:forEach var="ee" items="${requestScope.serviceList}" &g ...
- 【转】 Linux/Unix 进程间通信的各种方式及其比较
http://blog.csdn.net/guopengzhang/article/details/5528260 进程间通信就是在不同进程之间传播或交换信息,那么不同进程之间存在着什么双方都可以访问 ...
- zoj 1372
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1372 #include<iostream> #include& ...
- [Locked] 3Sum Smaller
3Sum Smaller Given an array of n integers nums and a target, find the number of index triplets i, j, ...
- MyEclipse里项目部署到tomcat上之后,tomcat webpps文件夹里为什么找不到这个项目
今天在MyEclipse中部署了一个java web项目,然后发现报404错误,跑到tomcat目录下的webapps文件夹里并发现没有这个项目,才发现MyEclipse没有写入webapp ...
- HDU2059(龟兔赛跑)
龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- ServletConfig
ServletConfig Servlet配置 比如web程序中的某一个Servlet需要配置一些初始化信息,需要在web.xml中进行配置 <servlet> <servlet-n ...
- 英蓓特Mars board的android4.0.3源码编译过程
英蓓特Mars board的android4.0.3源码编译过程 作者:StephenZhu(大桥++) 2013年8月22日 若要转载,请注明出处 一.编译环境搭建及要点: 1. 虚拟机软件virt ...
- JavaBean基础
JavaBean的概念 JavaBean是一种可重复使用.且跨平台的软件组件.JavaBean可分为两种:一种是有用户界面(UI,User Interface)的JavaBean:还有一种是没有用户界 ...