23 Merge k Sorted Lists

本文探讨了多种链表合并方法,包括连续两两合并、分治法及基于堆排序的k路合并算法。通过对比不同方法的执行效率,指出堆排序算法在解决多个有序链表合并问题上的优势。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

将k个列表排序汇合

输入:链表节点指针的向量

输出:链表节点的指针

思路:

假设有k个链表,每个链表的平均长度为l

1.连续merge2

2l+3l+4l+…+kl =  k*k*l/2

2.直接mergek

K*K*l

好像也差不多。

那就写个2merge试试吧

ListNode* mergeKLists(vector<ListNode*>& lists) {
        ListNode*total = NULL;
        for(ListNode* node : lists){
            total = mergeTwoLists(node,total);
        }
        return total;
    }
 

居然还没超时,还可以

执行用时 : 292 ms, 在Merge k Sorted Lists的C++提交中击败了16.59% 的用户

内存消耗 : 11.7 MB, 在Merge k Sorted Lists的C++提交中击败了0.81% 的用户

顺便一提,使用分治的时间复杂度和一个一个2merge的时间复杂度相同,起不到什么好作用。

那么该看正确的解法了:

基于堆排序的k路合并算法:

class Solution {
public:
    class priority{
        public:
        bool operator () (ListNode* a, ListNode* b){
            if(a == NULL)
                return false;
            else if(b == NULL)
                return true;
            else
                return a->val>b->val;
	   }
    };
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        if(lists.empty())
            return NULL;
        priority_queue<ListNode*, vector<ListNode*>, priority> help;
        ListNode* ret_val = new ListNode(0);
        auto temp_node = ret_val;
        for(auto i:lists)
            help.push(i);
        while(!help.empty()){
            auto temp = help.top();
            help.pop();
            if(temp != NULL){
                temp_node->next = temp;
                temp_node = temp_node->next;
                help.push(temp->next);
            }   
        }
        return ret_val->next;
    }
};

执行用时 : 84 ms, 在Merge k Sorted Lists的C++提交中击败了35.76% 的用户

内存消耗 : 11.9 MB, 在Merge k Sorted Lists的C++提交中击败了0.81% 的用户

利用ret_val当哨兵节点,然后利用队列,每次取出队列最前面的那一个的当前值,然后存入,然后队列进行重新排序,直到将k个链表都归并完成。这个方法的重点是要写一个完善的比较函数,然后将每个链表的表头指针存入优先级队列,然后根据优先级队列的性质获取当前k路里面的最小值。

需要注意的就是优先级队列的声明和使用

看到了一个暴力解法很有意思:就是将所有的链表直接合并,然后进行链表排序。。。时间复杂度就是排序的时间复杂度,K*L*K*L 应该是最高的时间复杂度了。

To merge k sorted linked lists, one approach is to repeatedly merge two of the linked lists until all k lists have been merged into one. We can use a priority queue to keep track of the minimum element across all k linked lists at any given time. Here's the code to implement this idea: ``` struct ListNode { int val; ListNode* next; ListNode(int x) : val(x), next(NULL) {} }; // Custom comparator for the priority queue struct CompareNode { bool operator()(const ListNode* node1, const ListNode* node2) const { return node1->val > node2->val; } }; ListNode* mergeKLists(vector<ListNode*>& lists) { priority_queue<ListNode*, vector<ListNode*>, CompareNode> pq; for (ListNode* list : lists) { if (list) { pq.push(list); } } ListNode* dummy = new ListNode(-1); ListNode* curr = dummy; while (!pq.empty()) { ListNode* node = pq.top(); pq.pop(); curr->next = node; curr = curr->next; if (node->next) { pq.push(node->next); } } return dummy->next; } ``` We start by initializing a priority queue with all the head nodes of the k linked lists. We use a custom comparator that compares the values of two nodes and returns true if the first node's value is less than the second node's value. We then create a dummy node to serve as the head of the merged linked list, and a current node to keep track of the last node in the merged linked list. We repeatedly pop the minimum node from the priority queue and append it to the merged linked list. If the popped node has a next node, we push it onto the priority queue. Once the priority queue is empty, we return the head of the merged linked list. Note that this implementation has a time complexity of O(n log k), where n is the total number of nodes across all k linked lists, and a space complexity of O(k).
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值