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;
    }
}

  

原文地址:https://www.cnblogs.com/asuran/p/7679751.html