0086.cpp (462B)
1 class Solution { 2 public: 3 ListNode *partition(ListNode *head, int x) { 4 ListNode hless, hmore; 5 ListNode *less = &hless, *more = &hmore; 6 7 for (ListNode *crnt = head; crnt; crnt = crnt->next) { 8 if (crnt->val < x) 9 less = less->next = crnt; 10 else 11 more = more->next = crnt; 12 } 13 14 less->next = hmore.next; 15 more->next = nullptr; 16 return hless.next; 17 } 18 };