86. Partition List - cocoder39/coco39_LC GitHub Wiki

86. Partition List

ListNode* partition(ListNode* head, int x) {
        ListNode left(0);
        ListNode right(0);
        ListNode *lp = &left;
        ListNode *rp = &right;
    
        while (head) {
            if(head->val < x){
                lp->next = head;
                lp = lp->next;
            }
            else{
                rp->next = head;
                rp = rp->next;
            }
            head = head->next;
        }
        lp->next = right.next;
        rp->next = nullptr;
        return left.next;
    }