/*
* Sort a linked list using insertion sort.
* */
public ListNode insertionSortList(ListNode head)
{
ListNode p = new ListNode(0);
ListNode phead = p;
while(head!=null)
{
p = phead; //reset p to the phead
while(p.next!=null)
{
if(p.next.val>head.val)
{
ListNode tmp = p.next;
p.next = head;
head = head.next; //here head next
p.next.next = tmp;
break;
}
p = p.next;
}
if(p.next == null)
{
p.next = head;
head = head.next;//here head next
p.next.next = null;
}
}
return phead.next;
}
//插入排序