-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathGFG_PartitionLL.cpp
171 lines (133 loc) · 3.28 KB
/
GFG_PartitionLL.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
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
/*
https://practice.geeksforgeeks.org/problems/partition-a-linked-list-around-a-given-value/1/#
Partition a Linked List around a given value
*/
// { Driver Code Starts
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
/* Link list node */
struct Node* partition(struct Node* head, int x);
struct Node {
int data;
struct Node* next;
Node(int x) {
data = x;
next = NULL;
}
};
/* Function to print linked list */
void printList(struct Node* node) {
while (node != NULL) {
printf("%d ", node->data);
node = node->next;
}
printf("\n");
}
/* Drier program to test above function*/
int main(void) {
int t;
cin >> t;
while (t--) {
struct Node* head = NULL;
struct Node* temp = NULL;
int n;
cin >> n;
for (int i = 0; i < n; i++) {
int value;
cin >> value;
if (i == 0) {
head = new Node(value);
temp = head;
} else {
temp->next = new Node(value);
temp = temp->next;
}
}
int k;
cin >> k;
// Solution ob;
head = partition(head, k);
printList(head);
}
return (0);
}
// } Driver Code Ends
// User function Template for C++
/*struct Node
{
int data;
struct Node* next;
Node(int x){
data = x;
next = NULL;
}
};*/
/***** *******/
struct Node* partition(struct Node* head, int x) {
// code here
if(!head || !head->next) return head;
Node* smhead = new Node(-1), *smp = smhead;
Node* eqhead = new Node(x), *eqp = eqhead;
Node* gthead = new Node(1), *gtp = gthead;
while(head)
{
if(head->data < x)
{
smp->next = head; smp = smp->next;
}
else if(head->data == x)
{
eqp->next = head; eqp = eqp->next;
}
else
{
gtp->next = head; gtp = gtp->next;
}
head = head->next;
}
gtp->next = nullptr; // make end of list as nullptr
if(eqp != eqhead) // if equal list exist
{
smp->next = eqhead->next; // merge small with equal
eqp->next = gthead->next; // merge equal with greater
return smhead->next;
}
smp->next = gthead->next;
return smhead->next;
}
/***** WITH QUEUE *******/
// struct Node* partition(struct Node* head, int x) {
// // code here
// if(!head || !head->next) return head;
// queue<int> small, greater;
// int equal = 0;
// Node* ptr = head;
// while(ptr)
// {
// if(ptr->data < x)
// small.push(ptr->data);
// else if(ptr->data > x)
// greater.push(ptr->data);
// else
// equal++;
// ptr = ptr->next;
// }
// ptr = head;
// while(!small.empty())
// {
// ptr->data = small.front(); small.pop();
// ptr = ptr->next;
// }
// while(equal--)
// {
// ptr->data = x;
// ptr = ptr->next;
// }
// while(!greater.empty())
// {
// ptr->data = greater.front(); greater.pop();
// ptr = ptr->next;
// }
// return head;
// }