10 lines concise C++ Solution


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

  • 0
    A
    This post is deleted!

Log in to reply
 

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