Leetcode 23. 合并K个升序链表merge-k-sorted-lists(合并k路有序链表 最小堆)

题目描述

合并k个已排序的链表并将其作为一个已排序的链表返回。分析并描述其复杂度。 
Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
 
思路:合并k路有序链表,可以用最小堆,然后每次送入堆中,取出最小的,并将该链表下一值取出放入
1、使用优先级队列模拟小根堆
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    // 优先级队列默认是大根堆,所以a>b生成小根堆
    struct comp{
        bool operator()(ListNode* a,ListNode* b){
            return a->val>b->val;
        }
    };
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        priority_queue<ListNode*,vector<ListNode*>,comp> q;
        int n=lists.size();
        int count=0;
        // 放入元素时要判断是否有链表为空
        for(int i=0;i<n;++i){
            if(lists[i]!=nullptr){
                q.push(lists[i]);
                count++;
            }
        }
        ListNode head(0);
        ListNode* p=&head;
        // 遍历知道堆中元素为空
        while(q.size()>0){
            ListNode* node=q.top();
            q.pop();
            p->next=node;
            p=p->next;
            ListNode* next=node->next;
            if(next!=nullptr)
                q.push(next);
        }
        return head.next;
    }
};

2、使用sort排序

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    static bool cmp(const ListNode *l1, const ListNode *l2)
    {
        return l1->val<l2->val;
    }
    ListNode *mergeKLists(vector<ListNode *> &lists) {
        int n = lists.size();
        ListNode *res = new ListNode(-1);
        if (n<1)
            return res->next;
        if (n<2)
            return lists[0];
        ListNode *p = res;
        int m = 0;
        for(int i=0;i<n;++i)
        {
            if(lists[i]==NULL)
            {
                swap(lists[m],lists[i]);
                m++;
            }
        }
        while(m < n-1)
        {
            sort(lists.begin()+m,lists.end(),cmp);
            p->next=lists[m];
            p=p->next;
            lists[m]=lists[m]->next;
            if(lists[m]==NULL)
            {
                m++;
            }
        }
        if(lists[m]!=NULL)
            p->next=lists[m];
        return res->next;
    }
};

 3、使用手写堆排序

class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        vector<ListNode*> stack;
        int length = lists.size();
        int n=0;
        for(int i=0;i<length;++i){
            if (lists[i]!=nullptr){
                stack.push_back(lists[i]);
                n++;
            }
        }
        build(stack, n);
        ListNode head;
        ListNode* p = &head;
        while(n>0){
            fix(stack,n,0);
            p->next = stack[0];
            p=p->next;
            stack[0]=stack[0]->next;
            if(stack[0]==nullptr){
                swap(stack[0],stack[n-1]);
                --n;
            }
        }
        return head.next;
    }

    void fix(vector<ListNode*>& lists, int n, int i){
        int j=i*2+1;
        ListNode* tmp=lists[i];
        while(j<n){
            if(j+1<n&&lists[j+1]->val<lists[j]->val)
                j=j+1;
            if(tmp->val<lists[j]->val)
                break;
            lists[i]=lists[j];
            i=j;
            j=i*2+1;
        }
        lists[i]=tmp;
    }

    void build(vector<ListNode*>& lists, int n) {
        for(int i=n/2-1;i>=0;--i){
            fix(lists,n,i);
        }
    }
};

 

posted @ 2020-04-28 22:38  鸭子船长  阅读(323)  评论(0编辑  收藏  举报