I think this is the most clear code


  • 0
    G
    public static ListNode insertionSortList(ListNode head) {
            if(head == null) return head;
            ListNode minHead = new ListNode(Integer.MIN_VALUE);
            ListNode last = head;
            while(last != null){
            	ListNode nextNode = last.next;
            	ListNode insertNode = minHead;
            	while(insertNode.next != null){
            		if(last.val < insertNode.next.val)
            			break;
            		insertNode = insertNode.next;
            	}
            	last.next = insertNode.next;
            	insertNode.next = last;
            	last = nextNode;
            }
            return minHead.next;
    	}

  • 0
    W

    smart to use a dummy node.


Log in to reply
 

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