1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class Solution { public: ListNode* partition(ListNode* head, int x) { ListNode* smallHead = new ListNode(0); ListNode* smallCurr = smallHead; ListNode* largeHead = new ListNode(0); ListNode* largeCurr = largeHead; while (head != nullptr) { if (head->val < x) { smallCurr->next = head; smallCurr = smallCurr->next; } else { largeCurr->next = head; largeCurr = largeCurr->next; } head = head->next; } smallCurr->next = largeHead->next; largeCurr->next = nullptr; return smallHead->next; } };
|