forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
32 lines (32 loc) · 857 Bytes
/
Solution.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
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* l1 = new ListNode();
ListNode* l2 = new ListNode();
ListNode* cur1 = l1;
ListNode* cur2 = l2;
while (head != nullptr) {
if (head->val < x) {
cur1->next = head;
cur1 = cur1->next;
} else {
cur2->next = head;
cur2 = cur2->next;
}
head = head->next;
}
cur1->next = l2->next;
cur2->next = nullptr;
return l1->next;
}
};