Clean C++ 6ms solution


  • 0
    class Solution {
    public:
        ListNode* partition(ListNode* head, int x) {
            ListNode left(0);
            ListNode right(0);
            ListNode* l = &left;
            ListNode* r = &right;
            ListNode* cur = head;
            while(cur){
                if(cur->val < x){
                    l->next = cur;
                    l = l->next;
                }
                else{
                    r->next = cur;
                    r = r->next;
                }
                cur = cur->next;
            }
            r->next = NULL;
            l->next = right.next;
            return left.next;
        }
    };
    

Log in to reply
 

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