leetcode148. Sort List(浅)

题目描述

Sort a linked list in O(n log n) time using constant space complexity.

Example 1:

Input: 4->2->1->3
Output: 1->2->3->4

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sort-list
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

代码

归并排序自顶向下

#include <iostream>
#include <vector>

using namespace std;

/// Definition for singly-linked list.
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class ListOp{

public:
    // 根据n个元素的数组arr创建一个链表, 并返回链表的头
    static ListNode* createLinkedList(const vector<int>& arr){

        if(arr.size() == 0)
            return NULL;

        ListNode* head = new ListNode(arr[0]);
        ListNode* curNode = head;
        for(int i = 1 ; i < arr.size() ; i ++){
            curNode->next = new ListNode(arr[i]);
            curNode = curNode->next;
        }
        return head;
    }

    // 打印以head为头结点的链表信息内容
    static void printLinkedList(ListNode* head){

        ListNode* curNode = head;
        while(curNode){
            cout << curNode->val << " -> ";
            curNode = curNode->next;
        }
        cout << "NULL" << endl;
    }

    // 释放以head为头结点的链表空间
    static void deleteLinkedList(ListNode* head){

        ListNode* curNode = head;
        while(curNode){
            ListNode* delNode = curNode;
            curNode = curNode->next;
            delete delNode;
        }
    }
};


/// Merge Sort Top Down
/// Time Complexity: O(nlogn)
/// Space Complexity: O(logn)
class Solution {
public:
    ListNode* sortList(ListNode* head) {

        if(head == NULL || head->next == NULL)
            return head;

        ListNode* slow = head;
        ListNode* fast = head->next;
        while(fast && fast->next){
            slow = slow->next;
            fast = fast->next->next;
        }

        ListNode* head2 = slow->next;
        slow->next = NULL;
        head = sortList(head);
        head2 = sortList(head2);
        return merge(head, head2);
    }

private:
    ListNode* merge(ListNode* a, ListNode* b){

        ListNode* dummyHead = new ListNode(-1);
        ListNode *p1 = a, *p2 = b, *p = dummyHead;
        while(p1 && p2)
            if(p1->val < p2->val){
                p->next = p1;
                p1 = p1->next;
                p = p->next;
                p->next = NULL;
            }
            else{
                p->next = p2;
                p2 = p2->next;
                p = p->next;
                p->next = NULL;
            }
        if(p1) p->next = p1;
        if(p2) p->next = p2;

        ListNode* ret = dummyHead->next;
        delete dummyHead;
        return ret;
    }
};


int main() {

    vector<int> arr = {4, 2, 1, 3};
    ListNode* head = Solution().sortList(ListOp::createLinkedList(arr));
    ListOp::printLinkedList(head);
    ListOp::deleteLinkedList(head);

    return 0;
}

思路二

归并排序自底向上

#include <iostream>
#include <vector>

using namespace std;

/// Definition for singly-linked list.
struct ListNode {
    int val;
    ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};

class ListOp{

public:
    // 根据n个元素的数组arr创建一个链表, 并返回链表的头
    static ListNode* createLinkedList(const vector<int>& arr){

        if(arr.size() == 0)
            return NULL;

        ListNode* head = new ListNode(arr[0]);
        ListNode* curNode = head;
        for(int i = 1 ; i < arr.size() ; i ++){
            curNode->next = new ListNode(arr[i]);
            curNode = curNode->next;
        }
        return head;
    }

    // 打印以head为头结点的链表信息内容
    static void printLinkedList(ListNode* head){

        ListNode* curNode = head;
        while(curNode){
            cout << curNode->val << " -> ";
            curNode = curNode->next;
        }
        cout << "NULL" << endl;
    }

    // 释放以head为头结点的链表空间
    static void deleteLinkedList(ListNode* head){

        ListNode* curNode = head;
        while(curNode){
            ListNode* delNode = curNode;
            curNode = curNode->next;
            delete delNode;
        }
    }
};


/// Merge Sort Bottom Up
/// Time Complexity: O(nlogn)
/// Space Complexity: O(1)
class Solution {
public:
    ListNode* sortList(ListNode* head) {

        if(head == NULL || head->next == NULL)
            return head;
        
        ListNode* dummyHead = new ListNode(-1);
        dummyHead->next = head;
        int sz = 1;
        while(true){
            ListNode *pre = dummyHead, *cur = pre;

            while(cur->next){
                cur = pre;
                for(int i = 0; i < sz && cur->next; i ++, cur = cur->next);

                if(cur->next){
                    ListNode* pre2 = cur;
                    for(int i = 0; i < sz && cur->next; i ++, cur = cur->next);
                    ListNode* next = cur->next, *head2 = pre2->next;
                    pre2->next = NULL, cur->next = NULL;

                    ListNode* tail;
                    pre->next = merge(pre->next, head2, tail);

                    pre = tail;
                    pre->next = next;
                }
                else if(pre == dummyHead){
                    sz = 0;
                    break;
                }
            }

            if(sz == 0) break;
            else sz *= 2;
        }

        ListNode* ret = dummyHead->next;
        delete dummyHead;
        return ret;
    }

private:
    ListNode* merge(ListNode* a, ListNode* b, ListNode* &tail){

        ListNode* dummyHead = new ListNode(-1);
        ListNode *p1 = a, *p2 = b, *p = dummyHead;
        while(p1 && p2)
            if(p1->val < p2->val){
                p->next = p1;
                p1 = p1->next;
                p = p->next;
                p->next = NULL;
            }
            else{
                p->next = p2;
                p2 = p2->next;
                p = p->next;
                p->next = NULL;
            }
        if(p1) p->next = p1;
        if(p2) p->next = p2;

        tail = p;
        while(tail->next) tail = tail->next;

        ListNode* ret = dummyHead->next;
        delete dummyHead;
        return ret;
    }
};


int main() {

    vector<int> arr = {4, 2, 1, 3};
    ListNode* head = Solution().sortList(ListOp::createLinkedList(arr));
    ListOp::printLinkedList(head);
    ListOp::deleteLinkedList(head);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值