-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSortedLinkedLists.cpp
More file actions
188 lines (154 loc) · 4.56 KB
/
MergeSortedLinkedLists.cpp
File metadata and controls
188 lines (154 loc) · 4.56 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// Given two sorted linked lists, we need to efficiently merge them into a single list:
// input1: {1,3,5}
// input2: {2,4,6,7}
// output: {1,2,3,4,5,6,7}
// There are a couple of ways to do this:
// We can create a brand new list and this will have a time and space complexity of O(n)
// If a new list is not needed, we can reduce the space ocmplexity to O(1). This is not possible
// with arrays because we can't modify links. With lists, we can just keep updating the "next"
// pointer to an appropriate value
//
#include <iostream>
#include <vector>
class Node {
public:
int data;
Node* next;
};
class LinkedList {
public:
LinkedList() {
}
LinkedList(Node* list)
: root(list) {
}
LinkedList(const std::vector<int>& vec)
: root(nullptr) {
root = new Node({vec[0], nullptr});
Node* prev = root;
for (size_t i = 1; i < vec.size(); ++i) {
Node* newNode = new Node({vec[i], nullptr});
prev->next = newNode;
prev = newNode;
}
prev->next = nullptr;
}
~LinkedList() {
std::cout << "Destroying: ";
cleanup(root);
std::cout << std::endl;
}
Node* getRoot() const {
return root;
}
Node*& getModifyableroot() {
return root;
}
void setRoot(Node* value) {
root = value;
}
void print() {
std::cout << "List : ";
Node* temp = root;
while (temp != nullptr) {
std::cout << temp->data << " ";
temp = temp->next;
}
std::cout << std::endl;
}
// Time complexity is O(n)
// Space complexity is also O(n)
LinkedList merge1(const LinkedList& other) {
Node* newroot = nullptr;
Node* newtail = nullptr;
Node* temp = nullptr;
Node* left = root;
Node* right = other.getRoot();
if (left->data < right->data) {
newroot = new Node({left->data, nullptr});
left = left->next;
} else {
newroot = new Node({right->data, nullptr});
right = right->next;
}
newtail = newroot;
while (left && right) {
if (left->data < right->data) {
temp = new Node({left->data, nullptr});
left = left->next;
} else {
temp = new Node({right->data, nullptr});
right = right->next;
}
newtail->next = temp;
newtail = temp; // this is imp to make sure the list is connected
}
while (left) {
temp = new Node({left->data, nullptr});
left = left->next;
newtail->next = temp;
newtail = temp;
}
while (right) {
temp = new Node({right->data, nullptr});
right = right->next;
newtail->next = temp;
newtail = temp;
}
return LinkedList(newroot);
}
LinkedList merge2(LinkedList& other) {
// We empty the list in other and merge it
// with this
Node* left = root;
Node* right = other.getModifyableroot();
LinkedList newlist = LinkedList(SortedMerge(left, right));
setRoot(nullptr);
other.setRoot(nullptr);
return newlist;
}
private:
Node* SortedMerge(Node* left, Node* right) {
if (left == nullptr)
return right;
if (right == nullptr)
return left;
Node* temp = nullptr;
if (left->data < right->data) {
temp = left;
temp->next = SortedMerge(left->next, right);
} else {
temp = right;
temp->next = SortedMerge(left, right->next);
}
return temp;
}
void cleanup(Node*& node) {
if (node) {
cleanup(node->next);
std::cout << node->data << " ";
delete node;
node = nullptr;
}
}
private:
Node* root;
};
int main(int argc, const char * argv[]) {
// O(n) space and time complexity
LinkedList obj1({1,3,5});
LinkedList obj2({2,4,6,7});
obj1.print();
obj2.print();
LinkedList obj3 = obj1.merge1(obj2);
obj3.print();
// O(n) time and O(1) space complexity
LinkedList obj4({1,3,5});
LinkedList obj5({2,4,6,7});
obj4.print();
obj5.print();
LinkedList obj6 = obj4.merge2(obj5);
obj6.print();
return 0;
}