Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.


比较自然地想到以下三种方法,设k个链表 每个链表n个元素。

1.暴力法

每次遍历k个表头找出最小的元素,O(k),共O(nk)个元素 总复杂度O(nk^2)

2.分治法

两两合并 

方便起见 设k=2^r,

第一层进行k/2次合并操作,操作规模为2n(两个规模为n的表),总操作数为kn,

第二次进行k/4次合并操作,操作规模为4n(两个规模为2n的表),总操作为kn,

……

最后一次进行1次合并操作,操作规模为kn(两个规模为kn/2的表),总操作为kn,

层数为r,则总复杂度为O(nk logk ) 

3. 逐一合并

第一次 两个规模为n的链表合并,操作规模为2n,

第二次 规模2n的链表与n的链表合并,操作规模为3n,

……

最后一次 规模(k-1)n的链表与n的链表合并,操作规模kn,

由等差数列求和公式 得复杂度O(nk^2)


因此 分治法复杂度明确更低 采用分治法更好、



class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {        
		int n = lists.size();
        return Helper(lists,0,n-1);
    }
    ListNode* Helper(vector<ListNode*>& lists,int begin,int end){
        if(begin>end){return NULL;}
        if(begin==end){return lists[begin];}
        if(begin+1==end){
			ListNode *newhead = NULL;
            ListNode *p = newhead;
            ListNode *p1 = lists[begin];
            ListNode *p2 = lists[end];
            
            if(p1==NULL||p2==NULL){
                if(p1==NULL) return p2;
                else return p1;
            }
            
            if(p1->val<=p2->val){
                    p = p1;
                    p1=p1->next;
                }
                else{
                    p= p2;
                    p2=p2->next;
                }
             newhead = p;
            while(p1!=NULL&&p2!=NULL){
                
                if(p1->val<=p2->val){
                    p->next = p1;
                    p1=p1->next;
                }
                else{
                    p->next= p2;
                    p2=p2->next;
                }
                p=p->next;
            }
            if(p1==NULL){p->next=p2;}
            else{p->next=p1;}
        return newhead;
        }
        vector<ListNode*> newlists;
        newlists.push_back(Helper(lists,begin,(begin+end)/2));
        newlists.push_back(Helper(lists,((begin+end)/2)+1,end));
        return Helper(newlists,0,1);
    }
};





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值