-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path86.cpp
More file actions
29 lines (29 loc) · 711 Bytes
/
Copy path86.cpp
File metadata and controls
29 lines (29 loc) · 711 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
// 1.cpp
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
if (head == nullptr || head->next == nullptr)
return head;
auto small = ListNode(0), big = ListNode(0);
auto p1 = &small, p2 = &big;
while (head) {
if (head->val < x) {
p1 = p1->next = head;
} else {
p2 = p2->next = head;
}
head = head->next;
}
p1->next = big.next;
p2->next = nullptr;
return small.next;
}
};