forked from haoel/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linkedListCycle.II.cpp
70 lines (54 loc) · 1.61 KB
/
linkedListCycle.II.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Source : https://oj.leetcode.com/problems/linked-list-cycle-ii/
// Author : Hao Chen
// Date : 2014-07-03
/**********************************************************************************
*
* Given a linked list, return the node where the cycle begins. If there is no cycle, return null.
*
* Follow up:
* Can you solve it without using extra space?
*
*
**********************************************************************************/
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
private:
ListNode *p1, *p2;
public:
bool hasCycle(ListNode *head) {
if (head==NULL) return false;
p1=head;
p2=head;
while (p1!=NULL && p2!=NULL){
p1=p1->next;
if (p2->next == NULL) return false;
p2=p2->next->next;
if (p1==p2) return true;
}
return false;
}
/*
* So, the idea is:
* 1) Using the cycle-chcking algorithm.
* 2) Once p1 and p1 meet, then reset p1 to head, and move p1 & p2 synchronously
* until p1 and p2 meet again, that place is the cycle's start-point
*/
ListNode *detectCycle(ListNode *head) {
if (hasCycle(head)==false){
return NULL;
}
p1 = head;
while (p1!=p2) {
p1 = p1->next;
p2 = p2->next;
}
return p1;
}
};