/**
* 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) {
return NULL;
}
ListNode* tmp1 = new ListNode(0), *pre1 = tmp1;
ListNode* tmp2 = new ListNode(0), *pre2 = tmp2;
ListNode* curr = head;
while (curr) {
if (curr->val < x) {
pre1->next = curr;
pre1 = pre1->next;
curr = curr->next;
} else {
pre2->next = curr;
curr = curr->next;
pre2 = pre2->next;
pre2->next = NULL;
}
}
pre1->next = tmp2->next;
return tmp1->next;
}
};