-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
is_list_cyclic.cc
101 lines (90 loc) · 2.85 KB
/
is_list_cyclic.cc
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <memory>
#include "list_node.h"
#include "test_framework/generic_test.h"
#include "test_framework/test_failure.h"
#include "test_framework/timed_executor.h"
using std::shared_ptr;
shared_ptr<ListNode<int>> HasCycle(const shared_ptr<ListNode<int>>& head) {
shared_ptr<ListNode<int>> fast = head, slow = head;
while (fast && fast->next) {
slow = slow->next, fast = fast->next->next;
if (slow == fast) {
// There is a cycle, so now let's calculate the cycle length.
int cycle_len = 0;
do {
++cycle_len;
fast = fast->next;
} while (slow != fast);
// Finds the start of the cycle.
auto cycle_len_advanced_iter = head;
while (cycle_len--) {
cycle_len_advanced_iter = cycle_len_advanced_iter->next;
}
auto iter = head;
// Both iterators advance in tandem.
while (iter != cycle_len_advanced_iter) {
iter = iter->next;
cycle_len_advanced_iter = cycle_len_advanced_iter->next;
}
return iter; // iter is the start of cycle.
}
}
return nullptr; // No cycle.
}
void HasCycleWrapper(TimedExecutor& executor,
const shared_ptr<ListNode<int>>& head, int cycle_idx) {
int cycle_length = 0;
if (cycle_idx != -1) {
if (!head) {
throw std::runtime_error("Can't cycle empty list");
}
shared_ptr<ListNode<int>> cycle_start, cursor = head;
while (cursor->next) {
if (cursor->data == cycle_idx) {
cycle_start = cursor;
}
cursor = cursor->next;
cycle_length += !!cycle_start;
}
if (cursor->data == cycle_idx) {
cycle_start = cursor;
}
if (!cycle_start) {
throw std::runtime_error("Can't find a cycle start");
}
cursor->next = cycle_start;
cycle_length++;
}
shared_ptr<ListNode<int>> result =
executor.Run([&] { return HasCycle(head); });
if (cycle_idx == -1) {
if (result != nullptr) {
throw TestFailure("Found a non-existing cycle");
}
} else {
if (result == nullptr) {
throw TestFailure("Existing cycle was not found");
}
auto cursor = result;
do {
cursor = cursor->next;
cycle_length--;
if (!cursor || cycle_length < 0) {
throw TestFailure(
"Returned node does not belong to the cycle or is not the "
"closest node to the head");
}
} while (cursor != result);
if (cycle_length != 0) {
throw TestFailure(
"Returned node does not belong to the cycle or is not the closest "
"node to the head");
}
}
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"executor", "head", "cycle_idx"};
return GenericTestMain(args, "is_list_cyclic.cc", "is_list_cyclic.tsv",
&HasCycleWrapper, DefaultComparator{}, param_names);
}