insertion-sort-list使用插入排序对链表进行排序

代码:

package com.niuke.p5;

public class Solution {
    public ListNode insertionSortList(ListNode head) {
        
        ListNode dumpy = new ListNode(Integer.MIN_VALUE);
        ListNode cur = head;
        ListNode pre = dumpy;
        while(cur != null) {
            ListNode next = cur.next;
            pre = dumpy;
            while(pre.next != null && pre.next.val < cur.val) {
                pre = pre.next;
            }
            cur.next = pre.next;
            pre.next = cur;
            cur = next;
        }
        return dumpy.next;
    }

}

 

posted @ 2020-05-31 00:46  alittlecomputer  阅读(160)  评论(0编辑  收藏  举报