/**
* 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 == nullptr || head->next == nullptr)
return head;
ListNode* small, *big, *smallHead=new ListNode(-1), *bigHead=new ListNode(-1);
small=smallHead;
big=bigHead;
while(head!=nullptr){
if(head->val<x){
small->next=head;
small=small->next;
}
else{
big->next=head;
big=big->next;
}
head=head->next;
}
big->next=nullptr;
small->next=bigHead->next;
return smallHead->next;
}
};