Sort a linked list using insertion sort.
1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * struct ListNode *next;6 * };7 */8 structlistnode* Insertionsortlist (structlistnode*head)9 {Ten structListNode *p; Onep=head; A intCount=0; - while(p!=NULL) - { thecount++; -P=p->Next; - } - + int*Array; -Array= (int*)malloc(count*sizeof(int)); + Ap=head; at intI=0, j,k; - while(p!=NULL) - { -Array[i]=p->Val; -P=p->Next; -i++; in } - to for(i=1; i<count;i++) + { - for(j=i-1; j>=0; j--) the { * if(array[j]<Array[i]) $ Break;Panax Notoginseng } - the if(j!=i-1) + { A inttmp=Array[i]; the for(k=i-1; k>j;k--) + { -array[k+1]=Array[k]; $ } $array[k+1]=tmp; - } - } the -I=0;Wuyi structListNode *Q; theq=head; - while(q!=NULL) Wu { -Q->val=Array[i]; AboutQ=q->Next; $i++; - } - - A returnhead; +}
Leecode-insertion Sort List