/* * 147. Insertion Sort List * 2016-6-1 by Mingyang * insertion sort的基本思路要有,两个while循环,一次过,对于每一个值与pree开始的
* list的节点依次比较,然后分别进行插入操作 */
public ListNode insertionSortList(ListNode head) { if(head==null||head.next==null) return head; ListNode pre=new ListNode(-1); while(head!=null){ if(pre.next==null){ pre.next=new ListNode(head.val); }else{ ListNode pree=pre; while(pree.next!=null&&pree.next.val<head.val){ pree=pree.next; } ListNode temp=pree.next; pree.next=new ListNode(head.val); pree.next.next=temp; } head=head.next; } return pre.next; }