C++ concise solution.


  • 3
    C
    ListNode* partition(ListNode* head, int x) {
        ListNode *dummy1 = new ListNode(0), *dummy2 = new ListNode(0), *node1, *node2;
        node1 = dummy1;
        node2 = dummy2;
        while (head) {
            if (head->val < x) {
                node1->next = head;
                node1 = node1->next;
            } else {
                node2->next = head;
                node2 = node2->next;
            }
            head = head->next;
        }
        node2->next = NULL;
        node1->next = dummy2->next;
        return dummy1->next;
    }

Log in to reply
 

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