-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartitionLLIntoGreaterLesserThanPivot.cpp
96 lines (77 loc) · 2.09 KB
/
PartitionLLIntoGreaterLesserThanPivot.cpp
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
// Partition a linked list around a value x, such that all nodes
// less than x come before all nodes greater than or equal to x.
//
// Complier: Visual Studio 2013 (v120)
#include <iostream>
#include <memory>
#include <vector>
using namespace std;
template <typename T>
struct Node {
T val;
shared_ptr<Node<T>> next;
Node(T val, shared_ptr<Node<T>> next) : val(val), next(next) {}
};
template <typename T>
void partition(shared_ptr<Node<T>> *node, T pivot) {
shared_ptr<Node<T>> greater = nullptr, less = nullptr;
while (*node) {
auto next = (*node)->next;
if ((*node)->val > pivot) {
(*node)->next = greater;
greater = (*node);
} else {
(*node)->next = less;
less = (*node);
}
// Move to next element
*node = next;
}
// Check there are elements in the greater list to append to.
if (!(greater)) {
*node = less;
return;
}
// Scan to end of greater list and append the less list.
auto greater_tail = greater;
while (greater_tail->next)
greater_tail = greater_tail->next;
greater_tail->next = less;
*node = greater;
}
template <typename T>
shared_ptr<Node<T>> convert_to_linked_list(const vector<T> &vec) {
if (vec.empty())
return nullptr;
shared_ptr<Node<T>> head = nullptr, tail = nullptr;
for (const T &val : vec) { // By ref to avoid copying
auto current = shared_ptr<Node<T>>(new Node<T>(val, nullptr));
// If head exists append to tail else set as head.
(head) ? tail->next = current: head = current;
tail = current;
}
return head;
}
template <typename T>
void display(const shared_ptr<Node<T>> &head) {
auto current = head;
cout << endl << "start->";
while (current) {
cout << current->val << "->";
current = current->next;
}
cout << "end" << endl;
}
int main()
{
vector<int> values { 5, 10, 3, 6, 8, 4, 2, 8, 7, 6, 3, 2, 1, 2, 3, 4 };
auto list = convert_to_linked_list(values);
cout << "Before" << endl;
display(list);
partition(&list, 5);
cout << "After" << endl;
display(list);
cout << endl << "[Press enter to exit]" << endl;
cin.ignore();
return 0;
}