-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartition-list.cpp
44 lines (41 loc) · 1.28 KB
/
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
35
36
37
38
39
40
41
42
43
44
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
ListNode *lessHead = NULL, *rightHead = NULL;
ListNode *lessEnd = NULL, *rightEnd = NULL;
while (head != NULL) {
//cout << head << endl;
if (head->val < x){
if(lessEnd == NULL){
lessHead = head;
lessEnd = head;
}
else{
lessEnd->next = head;
lessEnd = head;
}
}
else{
if(rightEnd == NULL){
rightHead = head;
rightEnd = head;
}
else{
rightEnd->next = head;
rightEnd = head;
}
}
head = head->next;
}
if (rightEnd!=NULL){
rightEnd->next = NULL;
}
if (lessHead == NULL){
return rightHead;
}
else{
lessEnd->next = rightHead;
return lessHead;
}
}
};