-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpartition_list.cpp
More file actions
42 lines (34 loc) · 901 Bytes
/
partition_list.cpp
File metadata and controls
42 lines (34 loc) · 901 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
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* ls = nullptr;
ListNode* l_tail;
ListNode* gt = nullptr;
ListNode* g_tail;
ListNode* search = head;
while(search){
ListNode* next = search->next;
search->next = nullptr;
if(search->val < x){
if(!ls)
ls = search;
else
l_tail->next = search;
l_tail = search;
}else{
if(!gt)
gt = search;
else
g_tail->next = search;
g_tail = search;
}
search = next;
}
if(!ls)
return gt;
else{
l_tail->next = gt;
return ls;
}
}
};