1. 程式人生 > >【LeetCode】147. 對連結串列進行插入排序 結題報告 (C++)

【LeetCode】147. 對連結串列進行插入排序 結題報告 (C++)

題目描述:

對連結串列進行插入排序。

插入排序的動畫演示如上。從第一個元素開始,該連結串列可以被認為已經部分排序(用黑色表示)。 每次迭代時,從輸入資料中移除一個元素(用紅色表示),並原地將其插入到已排好序的連結串列中。

插入排序演算法:

插入排序是迭代的,每次只移動一個元素,直到所有元素可以形成一個有序的輸出列表。 每次迭代中,插入排序只從輸入資料中移除一個待排序的元素,找到它在序列中適當的位置,並將其插入。 重複直到所有輸入資料插入完為止。  

示例 1:

輸入: 4->2->1->3 輸出: 1->2->3->4 示例 2:

輸入: -1->5->3->4->0 輸出: -1->0->3->4->5

解題方案:

自己寫的程式碼感覺還行,時間還過得去,定義的引數有點多了,懶得去做優化了。

程式碼:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* insertionSortList(ListNode* head) {
        if(!head || !head->next)
            return head;
        ListNode* p = head;
        ListNode* q;
        ListNode* cur;
        ListNode* pre;
        while(p->next){
            q = p->next;
            p->next = q->next;
            if(q->val < head->val){
                q->next = head;
                head = q;
                continue;
            }
            cur = head;
            pre = head;
            while(pre != p && q->val > cur->val){
                pre = cur;
                cur = cur->next;
            }
            q->next = pre->next;
            pre->next = q;
            if(pre == p)
                p = p->next;
        }
        return head;
    }
};

官網給的時間開銷最小的程式碼:

這TMD不是在瞎鬧嘛???

class Solution {
public:
    ListNode* insertionSortList(ListNode* head) {
        vector<int> v;
        for (ListNode *p1=head;p1!=NULL;p1=p1->next){
            v.push_back(p1->val);
        }
        sort(v.begin(),v.end());
        int i=0;
        for (ListNode *p1=head;p1!=NULL;p1=p1->next){
            p1->val=v[i];
            i++;
        }
        return head;
    }
};

另一種方法:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    ListNode* insertionSortList(ListNode* head) {
        if (!head) return head;
        ListNode* victim=new ListNode(0);
        victim->next=head;
        ListNode* test=head->next;
        while (test)
        {
            if (test->val>=head->val)
            {
                head=head->next;
                test=head->next;
            }
            else
            {
                head->next=test->next;
                test->next=NULL;
                ListNode* dummy=victim;
                while (true)
                {
                    if (dummy->next->val<test->val)
                    {
                        dummy=dummy->next;
                    }
                    else
                    {
                        ListNode* vic=dummy->next;
                        dummy->next=test;
                        test->next=vic;
                        break;
                    }
                }
                //head=head->next;
                test=head->next;
            }
        }
        return victim->next;
    }
    
};