My simple c++ solution


  • 0
    K
    class Solution {
    public:
    ListNode* partition(ListNode* head, int x) {
        if(!head || !head->next)return head;
        ListNode * p1=new ListNode(1), *p2=new ListNode(1), *head2, *head1;
        head1=p1;
        head2=p2;
        while(head){
            if(head->val < x){
                p1->next=new ListNode(head->val);
                
                p1=p1->next;
            }
            else {
                p2->next=new ListNode(head->val);
                p2=p2->next;
            }
            head=head->next;
        }
        p1->next=head2->next;
        return head1->next;
    }
    

    };


Log in to reply
 

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