/**
* 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) {
if(head==NULL or head->next==NULL){
return head;
}
else{
ListNode* less = NULL;
ListNode* less_poi = NULL;
ListNode* gre = NULL;
ListNode* gre_poi = NULL;
while(head!=NULL){
if((head->val)<x){
if(less==NULL){
less = head;
less_poi = less;
head = head->next;
}
else{
less->next = head;
less = less->next;
head = head->next;
}
}
else{
if(gre==NULL){
gre = head;
gre_poi = gre;
head = head->next;
}
else{
gre->next = head;
gre = gre->next;
head = head->next;
}
}
}
if(less==NULL){
return gre_poi;
}
else{
less->next = gre_poi;
return less_poi;
}
}
}
};