-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
reverse_sublist.cc
29 lines (26 loc) · 949 Bytes
/
reverse_sublist.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
#include "list_node.h"
#include "test_framework/generic_test.h"
shared_ptr<ListNode<int>> ReverseSublist(shared_ptr<ListNode<int>> L, int start,
int finish) {
auto dummy_head = make_shared<ListNode<int>>(0, L);
auto sublist_head = dummy_head;
int k = 1;
while (k++ < start) {
sublist_head = sublist_head->next;
}
// Reverses sublist.
auto sublist_iter = sublist_head->next;
while (start++ < finish) {
auto temp = sublist_iter->next;
sublist_iter->next = temp->next;
temp->next = sublist_head->next;
sublist_head->next = temp;
}
return dummy_head->next;
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"L", "start", "finish"};
return GenericTestMain(args, "reverse_sublist.cc", "reverse_sublist.tsv",
&ReverseSublist, DefaultComparator{}, param_names);
}