链表插入排序

/**
 * Definition for ListNode.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int val) {
 *         this.val = val;
 *         this.next = null;
 *     }
 * }
 */ 
public class Solution {
    /**
     * @param head: The first node of linked list.
     * @return: The head of linked list.
     */
    public ListNode insertionSortList(ListNode head) {
        // write your code here
        if(head == null){
            return null;
        }
        ListNode result = new ListNode(-1);
        ListNode pre = result;
        ListNode cur = head;
        while(cur!= null){
            ListNode next = cur.next;
            pre = result;
            while(pre.next!=null&& pre.next.val<=cur.val){
                pre = pre.next;
            }
            cur.next = pre.next;
            pre.next = cur;
            cur = next;
        }
        return result.next;
    }
}

  

原文地址:https://www.cnblogs.com/wangnanabuaa/p/5149730.html