-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
do_terminated_lists_overlap.cc
83 lines (68 loc) · 2.08 KB
/
do_terminated_lists_overlap.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
#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;
int Length(shared_ptr<ListNode<int>> L);
void AdvanceListByK(int k, shared_ptr<ListNode<int>>* L);
shared_ptr<ListNode<int>> OverlappingNoCycleLists(
shared_ptr<ListNode<int>> l0, shared_ptr<ListNode<int>> l1) {
int l0_len = Length(l0), l1_len = Length(l1);
// Advances the longer list to get equal length lists.
AdvanceListByK(abs(l0_len - l1_len), l0_len > l1_len ? &l0 : &l1);
while (l0 && l1 && l0 != l1) {
l0 = l0->next, l1 = l1->next;
}
return l0; // nullptr implies there is no overlap between l0 and l1.
}
int Length(shared_ptr<ListNode<int>> L) {
int length = 0;
while (L) {
++length, L = L->next;
}
return length;
}
// Advances L by k steps.
void AdvanceListByK(int k, shared_ptr<ListNode<int>>* L) {
while (k--) {
*L = (*L)->next;
}
}
void OverlappingNoCycleListsWrapper(TimedExecutor& executor,
shared_ptr<ListNode<int>> l0,
shared_ptr<ListNode<int>> l1,
shared_ptr<ListNode<int>> common) {
if (common) {
if (l0) {
auto i = l0;
while (i->next) {
i = i->next;
}
i->next = common;
} else {
l0 = common;
}
if (l1) {
auto i = l1;
while (i->next) {
i = i->next;
}
i->next = common;
} else {
l1 = common;
}
}
auto result = executor.Run([&] { return OverlappingNoCycleLists(l0, l1); });
if (result != common) {
throw TestFailure("Invalid result");
}
}
// clang-format off
int main(int argc, char* argv[]) {
std::vector<std::string> args {argv + 1, argv + argc};
std::vector<std::string> param_names {"executor", "l0", "l1", "common"};
return GenericTestMain(args, "do_terminated_lists_overlap.cc", "do_terminated_lists_overlap.tsv", &OverlappingNoCycleListsWrapper,
DefaultComparator{}, param_names);
}
// clang-format on