A stupid Js solution


  • 0
    G
    var insertionSortList = function(head) {
        var num =0;
        var dummy = new ListNode(0);
        dummy.next = head;
        var currPrev = dummy;
        
        while(currPrev!==null&&currPrev.next!==null){
        
            var changed = false;
            comPrev = dummy, currNext = currPrev.next.next; 
            for(var i = 0; i< num; i++){
                if(currPrev.next.val < comPrev.next.val){
                    //insert
                    var comTemp =comPrev.next;
                    var currTemp = currPrev.next.next;
                    comPrev.next = currPrev.next
                    comPrev.next.next = comTemp;
                    //
                    currPrev.next =  currTemp;
                    changed = true;
                    break;
                }
                comPrev = comPrev.next;
            }
            num++;
            if(!changed) currPrev = currPrev.next;
        }
        return dummy.next;
        
    };
    

Log in to reply
 

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