Sort a linked list using insertion sort.
PS: Need to consider left as head and only a number, at this time left->==null, if Right<left should update left.
Compare P->next->val and Right->val to avoid the need to record Prenode
1 /**2 * Definition for singly-linked list.3 * struct ListNode {4 * int val;5 * ListNode *next;6 * ListNode (int x): Val (x), Next (NULL) {}7 * };8 */9 classSolution {Ten Public: OneListNode *insertionsortlist (ListNode *head) { AListNode *left=null,*right=head; - if(head==NULL) - returnhead; theleft=head; -Right=head->Next; -left->next=NULL; - + while(right!=NULL) { -ListNode *p=Left ; +ListNode *cur=Right ; A at while(p!=null&&p->next!=null&&p->next->val<right->val) -P=p->Next; -Right=right->Next; - if(p->val<=cur->val) { -Cur->next=p->Next; -p->next=cur; in}Else{ -cur->next=p; toleft=cur; + } - the * } $ returnLeft ;Panax Notoginseng } -};
insertion-sort-list--linked list, insert sort, list insert