-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy path2_check_cycle.cpp
56 lines (47 loc) · 1.49 KB
/
2_check_cycle.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
/**
* Check if the linked list contains a cycle
*
* Time Complexity: O(n)
* Space Complexity: O(1)
*
* * What I learned:
*
* ** Two Runners approach to check cycle exists
* : keep two pointers to nodes (runners)
* - slowRunner: moves one node at a time
* - fastRunner: moves two nodes at a time
* => if linked list has a cycle, fastRunner will catch up with slowRunner
* if no cycle, fastRunner reaches the end of list
*
*/
#include <bits/stdc++.h>
using namespace std;
/**
* check if the linked list contains a cycle
*/
bool containsCycle(const LinkedListNode* firstNode) {
// if linked list is empty, no cycle
if (!firstNode) {
return false;
}
// create two pointers to traverse through list in different speeds
const LinkedListNode* fastRunner = firstNode;
const LinkedListNode* slowRunner = firstNode;
// traverse two runners through list
while (true) {
// if any runner reaches end of linked list
if (!slowRunner -> next_ || !fastRunner -> next_ -> next_) {
break;
}
// move fast runner and slow runner to next node
slowRunner = slowRunner -> next_;
fastRunner = fastRunner -> next_ -> next_;
// if fast and slow runner meet at some point
if (fastRunner == slowRunner) {
// there is cycle
return true;
}
}
// there are no cycles
return false;
}