-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinkedListCycle2.cpp
More file actions
56 lines (54 loc) · 1.47 KB
/
Copy pathLinkedListCycle2.cpp
File metadata and controls
56 lines (54 loc) · 1.47 KB
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
#include <set>
using namespace std;
struct ListNode{
int val;
ListNode *next;
ListNode(int x):val(x),next(NULL){}
};
class Solution {
public:
/* Sol 0
* High space complexity.(O(n))
* Using a set to store the address.
* O(n) time complexity.
*/
ListNode *detectCycle(ListNode *head) {
set<ListNode*> s;
if(!head) return nullptr;
ListNode *p = head;
while(p != nullptr && s.find(p) != s.end()){
s.insert(p);
p = p->next;
}
return p;
}
/* Sol 1
* Detect cycle using two pointers: fastP moves two steps once while
* slowP moves only one step.
* Then set fastP to head and move one step everytime.Then they will meet
* at the entry.
* WHY? Just draw a picture.
* O(1) space complexity and O(n) time complexity.
*/
ListNode *slowP,*fastP;
ListNode *detectCycle1(ListNode *head){
if(!hasCycle(head)) return nullptr;
fastP = head;
while(fastP != slowP){
fastP = fastP->next;
slowP = slowP->next;
}
return slowP;
}
bool hasCycle(ListNode *head) {
if(!head || !head->next) return false;
slowP = head,fastP = head;
while(slowP != nullptr && fastP != nullptr){
if(!fastP->next) return false;
slowP = slowP->next;
fastP = fastP->next->next;
if(slowP == fastP) return true;
}
return false;
}
};