[LeetCode]147. Insertion Sort List链表排序

插入排序的基本思想

把排好的放在一个新的变量中,每次拿出新的,排进去

这个新的变量要有超前节点,因为第一个节点可能会有变动

public ListNode insertionSortList(ListNode head) {
        if (head==null||head.next==null) return head;
        ListNode dummy = new ListNode(0);
        ListNode h = head;
        ListNode next;
        ListNode temp;
        while (h!=null)
        {
            next = h.next;
            temp = dummy;
            while (temp.next!=null&&h.val>temp.next.val) {
                temp = temp.next;
            }
            h.next = temp.next;
            temp.next = h;
            h = next;
        }
      
原文地址:https://www.cnblogs.com/stAr-1/p/8444336.html