1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
class Solution { public: ListNode* partition(ListNode* head, int x) { ListNode smaller, larger, *s = &smaller, *l = &larger; for (auto p = head; p; p = p->next) { if (p->val < x) { s->next = p; s = s->next; } else { l->next = p; l = l->next; } } s->next = larger.next; l->next = nullptr; return smaller.next; } };
|