-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjg_0204.cpp
More file actions
36 lines (32 loc) · 758 Bytes
/
jg_0204.cpp
File metadata and controls
36 lines (32 loc) · 758 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
/**
* @file jg_0204.cpp
* @brief https://leetcode-cn.com/problems/partition-list-lcci/
* @author YongDu
* @date 2021-09-09
*/
class Solution {
public:
ListNode *partition(ListNode *head, int x) {
if (head == nullptr) {
return head;
}
ListNode *leftDummy = new ListNode();
ListNode *rightDummy = new ListNode();
ListNode *left = leftDummy;
ListNode *right = rightDummy;
ListNode *node = head;
while (node) {
if (node->val < x) {
left->next = node;
left = left->next;
} else {
right->next = node;
right = right->next;
}
node = node->next;
}
right->next = nullptr;
left->next = rightDummy->next;
return leftDummy->next;
}
};