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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
#include "linked-list-utils.h"
#define LLN LinkedListNode
#include <deque>
// Ex1
template <class T>
void insertBeg(LLN<T>*& head, const T& elem) {
head = new LLN<int>(elem, head);
}
void ex1() {
LLN<int>* head = new LLN<int>(2, new LLN<int>(3, new LLN<int>(4)));
printLinkedList(head);
insertBeg(head, 1);
printLinkedList(head);
freeList(head);
}
// Ex 2
template <class T>
void reverse(LLN<T>*& head) {
LLN<T>* newHead = nullptr;
LLN<T>* iter;
while (head != nullptr) {
iter = head->next;
head->next = newHead;
newHead = head;
head = iter;
}
head = newHead;
}
void ex2() {
LLN<int>* head = new LLN<int>(1, new LLN<int>(2, new LLN<int>(3, new LLN<int>(4, new LLN<int>(5)))));
printLinkedList(head);
reverse(head);
printLinkedList(head);
freeList(head);
}
// Ex 3
template <class T>
LLN<T>* inefficientMiddle(LLN<T>* head) {
std::deque<LLN<T>*> ptrs;
while (head != nullptr) {
ptrs.push_back(head);
head = head->next;
}
while (ptrs.size() > 2) {
ptrs.pop_front();
ptrs.pop_back();
}
return ptrs.front();
}
template <class T>
LLN<T>* middle(LLN<T>* head) {
LLN<T>* fast = head, *slow = head;
while (fast && fast->next) {
fast = fast->next->next;
slow = slow->next;
}
return slow;
}
void ex3() {
LLN<int>* head1 = new LLN<int>(1, new LLN<int>(2, new LLN<int>(3, new LLN<int>(4, new LLN<int>(5)))));
printLinkedList(head1);
LLN<int>* head2 = new LLN<int>(1, new LLN<int>(2, new LLN<int>(3, new LLN<int>(4))));
printLinkedList(head2);
std::cout << middle(head1)->data << std::endl;
std::cout << middle(head2)->data << std::endl;
freeList(head1);
freeList(head2);
}
// Ex4
template <class T>
void reorder(LLN<T>*& head) {
if (!head) return;
LLN<T>* iter = head, *plast = head, *tail = head;
while (iter->next != nullptr && iter->next->next != nullptr) {
plast = iter;
iter = iter->next;
while (plast->next->next != nullptr)
plast = plast->next;
tail->next = plast->next;
tail->next->next = iter;
tail = tail->next->next;
plast->next = nullptr;
}
}
void ex4() {
LLN<int>* head = new LLN<int>(1, new LLN<int>(2, new LLN<int>(3, new LLN<int>(4, new LLN<int>(5)))));
printLinkedList(head);
reorder(head);
printLinkedList(head);
freeList(head);
head = new LLN<int>(1, new LLN<int>(2, new LLN<int>(3, new LLN<int>(4, new LLN<int>(5, new LLN<int>(6))))));
printLinkedList(head);
reorder(head);
printLinkedList(head);
freeList(head);
head = new LLN<int>(1);
printLinkedList(head);
reorder(head);
printLinkedList(head);
freeList(head);
head = nullptr;
printLinkedList(head);
reorder(head);
printLinkedList(head);
freeList(head);
}
int main() {
ex4();
}
|