leetcode刷题记录 147.对链表进行插入排序
思路
传统插入排序的思路
代码
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* struct ListNode *next;
* };
*/
struct ListNode* insertionSortList(struct ListNode* head){
if(head==NULL||head->next==NULL) return head;
struct ListNode *find,*findprior,*current=head->next,*currentnext=current->next;
head->next=NULL;
while(current){
find=head,findprior=head;
while(find!=NULL&&find->val<=current->val){
if(find==head) find=find->next;
else{
findprior=find;
if(find!=NULL)
find=find->next;
}
}
if(find==NULL){
if(findprior!=NULL)
findprior->next=current;
current->next=NULL;
}
else if(find==findprior){
current->next=find;
head=current;
}
else{
current->next=find;
findprior->next=current;
}
current=currentnext;
if(currentnext!=NULL)
currentnext=currentnext->next;
}
return head;
}