/**
* 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.的更多相关文章

  1. 【LeetCode OJ】Palindrome Partitioning II

    Problem Link: http://oj.leetcode.com/problems/palindrome-partitioning-ii/ We solve this problem by u ...

  2. 【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 ...

  3. 【LeetCode OJ】Word Ladder II

    Problem Link: http://oj.leetcode.com/problems/word-ladder-ii/ Basically, this problem is same to Wor ...

  4. 【LEETCODE OJ】Single Number II

    Problem link: http://oj.leetcode.com/problems/single-number-ii/ The problem seems like the Single Nu ...

  5. 【LeetCode OJ】Word Break II

    Problem link: http://oj.leetcode.com/problems/word-break-ii/ This problem is some extension of the w ...

  6. LeetCode OJ——Pascal's Triangle II

    http://oj.leetcode.com/problems/pascals-triangle-ii/ 杨辉三角2,比杨辉三角要求的空间上限制Could you optimize your algo ...

  7. LeetCode OJ 45. Jump Game II

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  8. 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 ...

  9. LeetCode OJ 63. Unique Paths II

    Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How m ...

随机推荐

  1. c++子类继承父类的覆盖问题

    废话少说,先看一段代码! #include <iostream> #include <string> using namespace std; class A { public ...

  2. C++ Primer 随笔 Chapter 10 关联容器

    1.关联容器的类型:map(键-值对的集合,可理解为关联数组), set(单纯的键的集合), multimap(一个键对应多个值,键唯一), multiset(相同键可以是多个). 2.pair类型提 ...

  3. 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 ...

  4. 【转】 Android开发之EditText属性详解

    原文网址:http://blog.csdn.net/qq435757399/article/details/7947862 1.EditText输入的文字为密码形式的设置 (1)通过.xml里设置: ...

  5. Android设备信息、感应器检测

    近日产品已经上线,开始有时间来做自己的事情,于是就开始学习和巩固一些以前用过的或者学过的技术.昨天写了一个检测Android设备的序列号和IMEI以及感应器等等的一个Demo来跟大家分享一下. 在开发 ...

  6. ScrollView自动滑到底部

    // 自动滑动到底部 mScrollView.post(new Runnable() { @Override public void run() { mScrollView.fullScroll(Sc ...

  7. [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 ...

  8. HDU2059(龟兔赛跑)

    龟兔赛跑 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  9. hdu 4619 二分图最大匹配 ——最大独立集

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4619 #include <cstdio> #include <cmath> # ...

  10. Android中 判断是平板还是手机

    //是平板返回true 不是平板返回false public  boolean isTablet(Context context) { return (context.getResources().g ...