zoukankan      html  css  js  c++  java
  • [LeetCode] Merge k Sorted Lists

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

    Example:

    Input:
    [
      1->4->5,
      1->3->4,
      2->6
    ]
    Output: 1->1->2->3->4->4->5->6

    归并K个有序链表

    思路1:使用multiset存储每一个出现的元素。再将这些元素连接起来即可(vector也可以)

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* mergeKLists(vector<ListNode*>& lists) {
            multiset<int> mset;
            for (auto& list : lists)
            {
                ListNode* cur = list;
                while (cur)
                {
                    mset.insert(cur->val);
                    cur = cur->next;
                }
            }
            ListNode* head = nullptr;
            ListNode* node = nullptr;
            for (auto it = mset.begin(); it != mset.end(); ++it)
            {
                if (node)
                {
                    node->next = new ListNode(*it);
                    node = node->next;
                }
                else
                {
                    node = new ListNode(*it);
                    head = node;
                }
            }
            return head;
        }
    };

    思路2:堆排序,使用优先队列

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* mergeKLists(vector<ListNode*>& lists) {
            auto cmp = [](const ListNode* a, const ListNode* b) { return a->val > b->val; };
            priority_queue<ListNode*, vector<ListNode*>, decltype(cmp)> min_heap(cmp);
            for (auto& list : lists)
                if (list)
                    min_heap.push(list);
            ListNode* dummy = new ListNode(-1);
            ListNode* cur = dummy;
            while (!min_heap.empty())
            {
                ListNode* node = min_heap.top();
                min_heap.pop();
                cur->next = node;
                cur = cur->next;
                if (node->next)
                    min_heap.push(node->next);
            }
            return dummy->next;
        }
    };

    思路三:利用分治+递归

    /**
     * Definition for singly-linked list.
     * struct ListNode {
     *     int val;
     *     ListNode *next;
     *     ListNode(int x) : val(x), next(NULL) {}
     * };
     */
    class Solution {
    public:
        ListNode* mergeKLists(vector<ListNode*>& lists) {
            if (lists.empty())
                return NULL;
            while (lists.size() > 1)
            {
                lists.push_back(mergeTwoLists(lists[0], lists[1]));
                lists.erase(lists.begin());
                lists.erase(lists.begin());
            }
            return lists[0];
        }
        
        ListNode* mergeTwoLists(ListNode* l1, ListNode* l2)
        {
            if (l1 == NULL)
                return l2;
            if (l2 == NULL)
                return l1;
            if (l1->val < l2->val)
            {
                l1->next = mergeTwoLists(l1->next, l2);
                return l1;
            }
            else
            {
                l2->next = mergeTwoLists(l1, l2->next);
                return l2;
            }
        }
    };
  • 相关阅读:
    PointToPointNetDevice doesn't support TapBridgeHelper
    NS3系列—10———NS3 NodeContainer
    NS3系列—9———NS3 IP首部校验和
    NS3系列—8———NS3编译运行
    【习题 7-6 UVA
    【Good Bye 2017 C】 New Year and Curling
    【Good Bye 2017 B】 New Year and Buggy Bot
    【Good Bye 2017 A】New Year and Counting Cards
    【Educational Codeforces Round 35 D】Inversion Counting
    【Educational Codeforces Round 35 C】Two Cakes
  • 原文地址:https://www.cnblogs.com/immjc/p/9067897.html
Copyright © 2011-2022 走看看