LeetCode-Insertion Sort List

记清insertion sort 的方法

然后就是dummy!!!

public class Solution {
    public ListNode insertionSortList(ListNode head) {
        if ( head == null || head.next == null )
            return head;
        ListNode temp = head;
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        while ( temp.next != null ){
            ListNode cur = temp.next;
            if ( temp.val > temp.next.val){
                ListNode node = dummy.next;
                ListNode pre = dummy;
                while ( node.val <= cur.val ){
                    node = node.next;
                    pre = pre.next;
                }
                temp.next = cur.next;
                cur.next = node;
                pre.next = cur;
            }
            else
                temp = temp.next;
        }
        return dummy.next;
    }
}


你可能感兴趣的:(LeetCode-Insertion Sort List)