Sort of insertion sort list

Sort the list insertion sort algorithm:

public static ListNode insertionSortList(ListNode head) {
        if (null == head || null == head.next){
            return head;
        }
        ListNode preHead = new ListNode(-1);
        preHead.next = head;
        ListNode cur = head;
        while (null != cur){
            ListNode pre = preHead;
            ListNode next = cur.next;
            while (null != next && cur.val <= next.val){
                next = next.next;
                cur = cur.next;
            }
            if (null == next){
                break;
            }
            while (pre.next.val <= next.val){
                pre = pre.next;
            }
            cur.next = next.next;
            next.next = pre.next;
            pre.next = next;
        }
        return preHead.next;
    }

Before sorting: 628,495,137

Sorted: 123,456,789

Guess you like

Origin www.cnblogs.com/earthhouge/p/11581268.html