-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0086-Partition_List.cpp
More file actions
174 lines (159 loc) · 3.4 KB
/
0086-Partition_List.cpp
File metadata and controls
174 lines (159 loc) · 3.4 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
/*******************************************************************************
* 0086-Partition_List.cpp
* Billy.Ljm
* 15 August 2023
*
* =======
* Problem
* =======
* https://leetcode.com/problems/partition-list/
*
* Given the head of a linked list and a value x, partition it such that all
* nodes less than x come before nodes greater than or equal to x.
*
* You should preserve the original relative order of the nodes in each of the
* two partitions.
*
* ===========
* My Approach
* ===========
* We'll literally partition it into two linked lists, and concatenate them at
* the end.
*
* This has a time complexity of O(n), and a space complexity of O(n), where
* n is the length of the linked list.
******************************************************************************/
#include <iostream>
#include <vector>
using namespace std;
/**
* << operator for vectors
*/
template <typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << "[";
for (const auto elem : v) {
os << elem << ",";
}
if (v.size() > 0) os << "\b";
os << "]";
return os;
}
/**
* Definition for singly - linked list.
*/
struct ListNode {
int val;
ListNode* next;
ListNode() : val(0), next(nullptr) {}
ListNode(int x) : val(x), next(nullptr) {}
ListNode(int x, ListNode* next) : val(x), next(next) {}
};
/**
* Creates a linked list from a vector
*/
ListNode* createLL(vector<int> elements) {
ListNode* head = new ListNode(elements[0]);
ListNode* crawler = head;
for (size_t i = 1; i < elements.size(); i++) {
crawler->next = new ListNode(elements[i]);
crawler = crawler->next;
}
return head;
}
/**
* Delete linked list
*/
void deleteLL(ListNode* head) {
ListNode* crawler;
while (head != nullptr) {
crawler = head;
head = head->next;
delete crawler;
}
}
/**
* Print linked list
*/
void printLL(ListNode* head) {
if (head != nullptr) {
std::cout << head->val << ",";
printLL(head->next);
}
}
/**
* Solution
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
if (head == nullptr or head->next == nullptr) return head;
ListNode* greater = nullptr, * lesser = nullptr;
ListNode* crawlh = head, * crawlg = greater, * crawll = lesser;
// partition
while (crawlh != nullptr) {
if (crawlh->val < x) {
if (lesser == nullptr) {
lesser = crawlh;
crawll = lesser;
}
else {
crawll->next = crawlh;
crawll = crawll->next;
}
}
else {
if (greater == nullptr) {
greater = crawlh;
crawlg = greater;
}
else {
crawlg->next = crawlh;
crawlg = crawlg->next;
}
}
crawlh = crawlh->next;
}
// concatenate
if (lesser == nullptr) {
crawlg = nullptr;
return greater;
}
else if (greater == nullptr) {
crawll = nullptr;
return lesser;
}
else {
crawll->next = greater;
crawlg->next = nullptr;
return lesser;
}
}
};
/**
* Test cases
*/
int main(void) {
Solution sol;
ListNode* head;
int x;
// test case 1
head = createLL({ 1,4,3,2,5,2 });
x = 3;
std::cout << "partition([";
printLL(head);
std::cout << "\b]," << x << ") = [";
printLL(sol.partition(head, x));
std::cout << "\b]" << std::endl;
deleteLL(head);
// test case 2
head = createLL({ 2,1 });
x = 2;
std::cout << "partition([";
printLL(head);
std::cout << "\b]," << x << ") = [";
printLL(sol.partition(head, x));
std::cout << "\b]" << std::endl;
deleteLL(head);
return 0;
}