A 9ms C++ Solution


  • 0
    Y
    ListNode* partition(ListNode* head, int x) {
            ListNode newHead(INT_MIN);
            newHead.next = head;
            ListNode *cur = head, *small = &newHead, *pre = &newHead;
            while(cur) {
                if(small->next && small->next->val < x) small = small->next;
                if(cur->val < x && pre->val >= x) {
                    pre->next = cur->next;
                    cur->next = small->next;
                    small->next = cur;
                    cur = pre->next;
                    small = small->next;
                } else {
                    cur = cur->next;
                    pre = pre->next;
                }
            }
            return newHead.next;
        }
    

Log in to reply
 

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