Concise recursive solution


  • 0
    S
    ListNode* partition(ListNode* head, int x) {
        if (!head)
            return NULL;
        head->next = partition(head->next, x);
        ListNode *node = head;
        if (node->val >= x) {
            while (node->next && node->next->val < x) {
                swap(node->val, node->next->val);
                node = node->next;
            }
        }
        return head;
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.