forked from vishal8113/Hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
partition-list.cpp
34 lines (34 loc) · 872 Bytes
/
partition-list.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
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
vector<int> v,n;
ListNode* curr=head;
while(curr!=nullptr){
v.push_back(curr->val);
curr=curr->next;
}
for(int i=0;i<v.size();i++){
if(x>v[i]){
n.push_back(v[i]);
}
}
for(int i=0;i<v.size();i++){
if(x<=v[i]){
n.push_back(v[i]);
}
}
ListNode* root=nullptr;
for(int i=0;i<n.size();i++){
if(root==nullptr){
root=new ListNode(n[i]);
continue;
}
ListNode* curr=root;
while(curr->next!=nullptr){
curr=curr->next;
}
curr->next=new ListNode(n[i]);
}
return root;
}
};