-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path86.cpp
More file actions
executable file
·50 lines (50 loc) · 1.44 KB
/
86.cpp
File metadata and controls
executable file
·50 lines (50 loc) · 1.44 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
/**
* 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) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* lesstmp = NULL;
ListNode* moretmp = NULL;
ListNode* tmp = head;
ListNode* ltmphead = NULL;
ListNode* mtmphead = NULL;
bool le = false;
bool me = false;
if (head == NULL)
return head;
while (tmp != NULL){
if (tmp->val < x){
if (le == false){
lesstmp = new ListNode(tmp->val);
le = true;
ltmphead = lesstmp;
}else{
lesstmp->next = new ListNode(tmp->val);
lesstmp = lesstmp->next;
}
}else{
if (me == false){
moretmp = new ListNode(tmp->val);
me = true;
mtmphead = moretmp;
}else{
moretmp->next = new ListNode(tmp->val);
moretmp = moretmp->next;
}
}
tmp = tmp->next;
}
if (le == false)
return mtmphead;
lesstmp->next = mtmphead;
return ltmphead;
}
};