86. Partition List
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
if(head == NULL)
return NULL;
ListNode* smaller_dummy = new ListNode(0);
ListNode* smaller_cur = smaller_dummy;
ListNode* greater_dummy = new ListNode(0);
ListNode* greater_cur = greater_dummy;
while(head != NULL){
if(head->val < x){
smaller_cur->next = head;
smaller_cur = smaller_cur->next;
}
else{
greater_cur->next = head;
greater_cur = greater_cur->next;
}
head = head->next;
}
smaller_cur->next = greater_dummy->next;
greater_cur->next = NULL;
return smaller_dummy->next;
}
};