【leetcode】147. Insertion Sort List
生活随笔
收集整理的這篇文章主要介紹了
【leetcode】147. Insertion Sort List
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Sort a linked list using insertion sort.
?
鏈表的插入排序。
需要創建一個虛擬節點。注意點就是不要節點之間斷了。
class Solution { public:ListNode* insertionSortList(ListNode* head) {if(head==NULL||head->next==NULL)return head;ListNode* newhead = new ListNode(-2147483648);newhead->next=head;ListNode* q=head;ListNode* p=head->next;while(p!=NULL){ListNode* s1=newhead;ListNode* s2=s1->next;while(s2!=p&&s2->val<=p->val){s1=s2;s2=s2->next;}if(s2!=p){s1->next=p;q->next=p->next;p->next=s2;p=q->next;}else{q=p;p=p->next;}}return newhead->next; } };?
轉載于:https://www.cnblogs.com/LUO77/p/5673247.html
總結
以上是生活随笔為你收集整理的【leetcode】147. Insertion Sort List的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 库乐队如何剪辑音频
- 下一篇: 王者荣耀买的皮肤可以退吗