Sort a linked list using insertion sort.
ListNode *insertionSortList(ListNode *head) {
if(NULL==head||NULL==head->next){
return head;
}
ListNode *pre=head,*pCur=head->next,*pNext=NULL,*pTemp=NULL;
head->next=NULL;
while (pCur)
{
pTemp=pCur->next;
if(pCur->val<head->val){
pCur->next=head;
head=pCur;
}
else
{
pre=head;
pNext=pre->next;
while (pNext&&pNext->val<pCur->val)
{
pre=pNext;
pNext=pNext->next;
}
pCur->next=pre->next;
pre->next=pCur;
}
pCur=pTemp;
}
return head;
}