[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 ...
随机推荐
- c++子类继承父类的覆盖问题
废话少说,先看一段代码! #include <iostream> #include <string> using namespace std; class A { public ...
- C++ Primer 随笔 Chapter 10 关联容器
1.关联容器的类型:map(键-值对的集合,可理解为关联数组), set(单纯的键的集合), multimap(一个键对应多个值,键唯一), multiset(相同键可以是多个). 2.pair类型提 ...
- awk合并文件一例
群里的朋友求助: $ cat file1a 1 2 3b 2 3 4c 3 4 5 $ cat file2d 你b 好c 吗 合并两个文件,需要实现: a 1 2 3b 2 3 4 好c 3 4 5 ...
- 【转】 Android开发之EditText属性详解
原文网址:http://blog.csdn.net/qq435757399/article/details/7947862 1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: ...
- Android设备信息、感应器检测
近日产品已经上线,开始有时间来做自己的事情,于是就开始学习和巩固一些以前用过的或者学过的技术.昨天写了一个检测Android设备的序列号和IMEI以及感应器等等的一个Demo来跟大家分享一下. 在开发 ...
- ScrollView自动滑到底部
// 自动滑动到底部 mScrollView.post(new Runnable() { @Override public void run() { mScrollView.fullScroll(Sc ...
- [Locked] Binary Tree Upside Down
Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes with a s ...
- HDU2059(龟兔赛跑)
龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- hdu 4619 二分图最大匹配 ——最大独立集
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619 #include <cstdio> #include <cmath> # ...
- Android中 判断是平板还是手机
//是平板返回true 不是平板返回false public boolean isTablet(Context context) { return (context.getResources().g ...