/* struct ListNode { int val; struct ListNode *next; ListNode(int x) : val(x), next(NULL) {} };*/ class Partition { public: ListNode* partition(ListNode* pHead, int x) { // write code here ListNode* lessHead,*lessTail; ListNode* greaterHead,*greaterTail; lessHead = lessTail = (ListNode*)malloc(sizeof(ListNode)); greaterHead = greaterTail = (ListNode*)malloc(sizeof(ListNode)); lessHead->next = greaterHead->next =nullptr; ListNode* cur = pHead; while(cur) { if(cur->val < x) { lessTail->next = cur; lessTail = lessTail->next; } else { greaterTail->next = cur; greaterTail = greaterTail->next; } cur = cur->next; } lessTail->next = greaterHead->next; greaterTail->next = nullptr; ListNode* returnHead = lessHead->next; free(lessHead); free(greaterHead); return returnHead; } };