The shortest Java 6ms (96.93%) simple logic solution


  • 0
    L
    public ListNode insertionSortList(ListNode head) {
        ListNode newH = new ListNode(0);
        newH.next = head;
        ListNode fNode = newH, curNode = newH;
        while(fNode != null && fNode.next != null && fNode.next.next != null){
            if(fNode.next.val <= fNode.next.next.val){
                fNode = fNode.next;
            }
            else{
                ListNode tmp = fNode.next.next;
                fNode.next.next = tmp.next;
                curNode = newH;
                while(curNode.next.val < tmp.val)
                    curNode = curNode.next;
                tmp.next = curNode.next;
                curNode.next = tmp;
            }
        }
        return newH.next;
    }

Log in to reply
 

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