147. Insertion Sort List

class Solution {
    public ListNode insertionSortList(ListNode head) {
        ListNode pre=new ListNode(0);
        ListNode p=head, q;
        while(p!=null)
        {
            q=pre;
            while(q.next!=null&&q.next.val<p.val)
                q=q.next;
            ListNode r=p;
            p=p.next;
            r.next=q.next;
            q.next=r;
        }
        return pre.next;
    }
}

  

posted @ 2017-10-17 04:41  Weiyu Wang  阅读(111)  评论(0编辑  收藏  举报