-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartition List.cpp
More file actions
46 lines (45 loc) · 1.19 KB
/
Copy pathPartition List.cpp
File metadata and controls
46 lines (45 loc) · 1.19 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
class Solution {
public:
ListNode* partition(ListNode* head1, int x) {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
ListNode *head2 = nullptr, *t1 = nullptr, *t2 = nullptr;
if(head1 == nullptr || head1->next == nullptr)
return head1;
while(head1 && head1->val >= x){
if(head2 == nullptr){
head2 = head1;
t2 = head2;
}
else{
t2->next = head1;
t2 = t2->next;
}
head1 = head1->next;
}
if(head1 == nullptr){
t2->next = nullptr;
return head2;
}
t1 = head1;
while(t1->next){
if(t1->next->val >= x){
if(head2 == nullptr){
head2 = t1->next;
t2 = head2;
}
else{
t2->next = t1->next;
t2 = t2->next;
}
t1->next = t1->next->next;
}
else
t1 = t1->next;
}
t1->next = head2;
if(t2)
t2->next = nullptr;
return head1;
}
};