/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {} };*/ #include <asm-generic/errno.h> class Partition { public: ListNode* partition(ListNode* pHead, int x) { if(pHead==NULL)return NULL; struct ListNode* lessthan=(struct ListNode*)malloc(sizeof(struct ListNode)); lessthan->next=NULL; struct ListNode* greaterthan=(struct ListNode*)malloc(sizeof(struct ListNode)); greaterthan->next=NULL; struct ListNode* cur=pHead; struct ListNode* next=NULL; if(cur)next=cur->next; struct ListNode* lesstail=lessthan; struct ListNode* greatertail=greaterthan; while(cur) { if(cur->val<x) { lesstail->next=cur; lesstail=cur; cur=next; if(next) next=next->next; else next=NULL; lesstail->next=NULL; } else { greatertail->next=cur; greatertail=cur; cur=next; if(next) { next=next->next; } else { next=NULL; } greatertail->next=NULL; } } struct ListNode* newhead=(struct ListNode*)malloc(sizeof(struct ListNode)); if(lessthan->next) { newhead->next=lessthan->next; lesstail->next=greaterthan->next; } else newhead->next=greaterthan->next; return newhead->next; } };