Concise JavaScript solution


  • 1
    L
    function insertionSortList(head) {
        var before = { val: -Number.MAX_VALUE, next: null };
        
        while (head) {
            var prev = before;
            
            // find prev
            while (prev.next && prev.next.val < head.val) {
                prev = prev.next;
            }
            
            var next = head.next;
            head.next = prev.next;
            prev.next = head;
            head = next;
        }
        
        return before.next;
    }

Log in to reply
 

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