mirror of https://github.com/doocs/leetcode.git
29 lines
720 B
C++
29 lines
720 B
C++
/**
|
|
* 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) {
|
|
ListNode* left = new ListNode(0);
|
|
ListNode* right = new ListNode(0);
|
|
ListNode* p1 = left;
|
|
ListNode* p2 = right;
|
|
for (; head; head = head->next) {
|
|
if (head->val < x) {
|
|
p1->next = head;
|
|
p1 = p1->next;
|
|
} else {
|
|
p2->next = head;
|
|
p2 = p2->next;
|
|
}
|
|
}
|
|
p1->next = right->next;
|
|
p2->next = nullptr;
|
|
return left->next;
|
|
}
|
|
}; |