Intuitive JavaScript solution


  • 0
    var partition = function(head, x) {
        const dummyLess = new ListNode();
        const dummyMore = new ListNode();
        let node = head;
        let less = dummyLess;
        let more = dummyMore;
        while (node) {
            if (node.val < x) {
                less = less.next = node;
            } else {
                more = more.next = node;
            }
            node = node.next;
        }
        less.next = dummyMore.next;
        more.next = null;
        return dummyLess.next;
    };
    

Log in to reply
 

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