-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathpartition_list.cpp
More file actions
48 lines (40 loc) · 828 Bytes
/
Copy pathpartition_list.cpp
File metadata and controls
48 lines (40 loc) · 828 Bytes
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
class Solution {
public:
void insert_node(ListNode *&head, ListNode *&tail, ListNode *node) {
if (NULL == head) {
head = node;
}
else {
tail->next = node;
}
tail = node;
}
ListNode *partition(ListNode *head, int x) {
if (NULL == head) {
return NULL;
}
ListNode *lt_head = NULL;
ListNode *lt_tail = NULL;
ListNode *ge_head = NULL;
ListNode *ge_tail = NULL;
while (head != NULL) {
if (head->val < x) {
insert_node(lt_head, lt_tail, head);
}
else {
insert_node(ge_head, ge_tail, head);
}
head = head->next;
}
if (ge_tail != NULL) {
ge_tail->next = NULL;
}
if (NULL == lt_head) {
return ge_head;
}
else {
lt_tail->next = ge_head;
return lt_head;
}
}
};