Insertion Sort List

容易出错的点

1 不是原位排序,惊讶!!

2 要重新定位pre!!

public class Solution {
    public ListNode insertionSortList(ListNode head) {
        // 想清楚怎么插入就很简单啦
        if(head==null|| head.next ==null) return head;
        ListNode h =  new ListNode(-1);
      //  h.next = head; // 不是原位排序
        ListNode  cur = head;
        while(cur!=null){
            // 要重新定义pre的指向!!!
            ListNode pre = h;
            ListNode t = cur.next;
            while(pre.next!=null && pre.next.val<=cur.val){
                pre = pre.next;
            }
            cur.next = pre.next;
            pre.next = cur;
            cur = t;
        }
        return h.next;
    }
}

 

posted @ 2015-04-09 13:06  世界到处都是小星星  阅读(95)  评论(0编辑  收藏  举报