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 @   alittlecomputer  阅读(161)  评论(0编辑  收藏  举报
点击右上角即可分享
微信分享提示