标签:else color sort ons solution bre lis public pre
1.
class Solution { public: ListNode* insertionSortList(ListNode* head) { ListNode ln(0);ln.next=head; ListNode *pre=&ln,*cur=head; while(cur) { if(cur->next&&cur->next->val<cur->val) { while(pre->next&&pre->next->val<cur->next->val) pre=pre->next; ListNode *tmp=pre->next; pre->next=cur->next; cur->next=cur->next->next; pre->next->next=tmp; pre=&ln; } else cur=cur->next; } return ln.next; } };
2.
class Solution { public: ListNode* insertionSortList(ListNode* head) { if(!head||!head->next) return head; ListNode* cur=head->next,*pre=head; while(cur) { ListNode* comp=head,*prec=nullptr; while(comp&&comp->val<cur->val) { prec=comp; comp=comp->next; if(comp==cur) { break; } } if(comp==cur) { pre=cur; cur=cur->next; continue; } if(!prec) { pre->next=cur->next; cur->next=head; head=cur; cur=pre->next; } else { pre->next=cur->next; prec->next=cur; cur->next=comp; cur=pre->next; } } return head; } };
leetcode 147 Insertion Sort List
标签:else color sort ons solution bre lis public pre
原文地址:https://www.cnblogs.com/LiuQiujie/p/12664981.html