
思路:
1.创建一个新的链表
2.对原链表进行头删
3.在新链表找插入的合适位置(保证插入后链表有序)
4.将头删的结点插入到新链表的合适位置处(头插操作和中间插尾插不相同)
typedef struct ListNode Node;
struct ListNode* insertionSortList(struct ListNode* head){
if(head == NULL || head->next == NULL)
{
return head;
}
Node *nhead = head;
head = head->next;
nhead->next = NULL;
while(head)
{
Node *cur = head;
head = head->next;
Node *insert = nhead,*pre = NULL;
while( insert && cur->val > insert->val)
{
pre = insert;
insert = insert->next;
}
if(insert == nhead)
{
cur->next = insert;
nhead = cur;
}
else
{
cur->next = insert;
pre->next = cur;
}
}
return nhead;
}