leetcode 147. Insertion Sort List ----- java

 

Sort a linked list using insertion sort.

插入排序。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode insertionSortList(ListNode head) {
        
        if( head == null )
            return null;
    
        ListNode node = head.next;
        head.next = null;
        ListNode start = head;

        while( no sdf sd sdfnull ){
            ListNode nn = node.next;
            ListNode first = start;
            if( node.val < first.val){
                node.next = first;
                start = node;
            }else{
                while( first.next != null && first.next.val < node.val )
                    first = first.next;
                node.next = first.next;
                first.next = node;

            }
            
            node = nn;

        }
        return start;
        
    }
}
原文地址:https://www.cnblogs.com/xiaoba1203/p/6070196.html