【Leetcode】【Hard】Merge k Sorted Lists

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

解题思路:

1、先取出k个list的首元素,每个首元素是对应list中的最小元素,组成一个具有k个结点的最小堆;o(k*logk)

2、此时堆顶元素就是所有k个list的最小元素,将其pop出,并用此最小元素所在list上的下一个结点(如果存在)填充堆顶,并执行下滤操作,重新构建堆。o(logk)

3、不断执行第二步,直到堆中元素为0。o(nlogk)

最终时间复杂度为o(nlogk),k表示list的个数,n表示所有list总结点数;

解题方法1:

使用C++ make_heap,push_heap,pop_heap函数;

步骤:

1、新建一个结点preHead,指向最终返回结果链表的首结点;新建一个curNode指针,用于操作新建链表;

2、将每个list的首指针装入一个新建的vector v中,注意判断list不为空;

3、调用make_heap函数将这些首指针构造成堆;需要编写greater比较函数;

4、循环操作,只要v不为空:

  (1)取出堆顶,放入新建链表的末端;

  (2)pop_heap,交换当前堆首尾元素,并下滤首元素;

  (3)pop_back,删除v最后一个元素(当前最小元素);

  (4)push_back,将当前最小元素的下一个元素加入堆尾;

  (5)push_heap,对堆尾元素进行上滤;

5、返回preHead->next;

代码:

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     ListNode* mergeKLists(vector<ListNode*>& lists) { //make_heap
12         ListNode prehead(0);
13         ListNode *curNode = &prehead;
14         vector<ListNode*> v;
15         for(int i = 0; i < lists.size(); i++){
16             if (lists[i])
17                 v.push_back(lists[i]);
18         }
19         make_heap(v.begin(), v.end(), heapComp); //vector -> heap data strcture
20
21         while(v.size() > 0){
22             curNode->next = v.front();
23             pop_heap(v.begin(), v.end(), heapComp);
24             v.pop_back();
25             curNode = curNode->next;
26             if(curNode->next) {
27                 v.push_back(curNode->next);
28                 push_heap(v.begin(), v.end(), heapComp);
29             }
30         }
31
32         return prehead.next;
33     }
34
35     static bool heapComp(ListNode* a, ListNode* b) {
36             return a->val > b->val;
37     }
38 };

解题方法2:

使用C++ priority_queue 构建最小堆;

步骤和上述基本一致;

代码

 1 /**
 2  * Definition for singly-linked list.
 3  * struct ListNode {
 4  *     int val;
 5  *     ListNode *next;
 6  *     ListNode(int x) : val(x), next(NULL) {}
 7  * };
 8  */
 9 class Solution {
10 public:
11     struct cmp {
12         bool operator()(const ListNode *n1, const ListNode *n2) {
13             return n1->val > n2->val;
14         }
15     };
16
17     ListNode *mergeKLists(vector<ListNode *> &lists) {
18         priority_queue<ListNode*, vector<ListNode *>, cmp > heap;
19         ListNode pre_head(0);
20         ListNode *curNode = &pre_head;
21         for (int i = 0; i < lists.size(); i++) {
22             if (lists[i])
23                 heap.push(lists[i]);
24         }
25
26         while (!heap.empty()){
27             curNode->next = heap.top();
28             heap.pop();
29             curNode = curNode->next;
30             if (curNode->next)
31                 heap.push(curNode->next);
32         }
33
34         return pre_head.next;
35     }
36 };
时间: 2024-10-17 08:20:30

【Leetcode】【Hard】Merge k Sorted Lists的相关文章

[LeetCode][JavaScript]Merge k Sorted Lists

https://leetcode.com/submissions/detail/28015840/ Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 直接使用了“Merge Two Sorted Lists”里写的方法. http://www.cnblogs.com/Liok3187/p/4514347.ht

【Leetcode长征系列】Merge k Sorted Lists

原题: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:两条两条地合并.时间复杂度为O(n),n为所有链表节点和. 代码: /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) :

【leetcode刷题笔记】Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解:最开始用了最naive的方法,每次在k个链表头中找出最小的元素,插入到新链表中.结果果断TLE了. 分析一下,如果这样做,每取出一个节点,要遍历k个链表一次,假设k个链表一共有n个节点,那么就需要O(nk)的时间复杂度. 参考网上的代码,找到了用最小堆的方法.维护一个大小为k的最小堆,存放当前k

【leetcode】Merge k Sorted Lists

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 采用优先队列priority_queue 把ListNode放入优先队列中,弹出最小指后,如果该ListNode有下一个元素,则把下一个元素放入到队列中 1 /** 2 * Definition for singly-linked list. 3 * stru

LeetCode: Merge k Sorted Lists [022]

[题目] Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. [题意] 合并K个有序链表 [思路] 归并 [代码] /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), n

leetcode 刷题之路 93 Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 将k个有序链表合并成一个有序链表. 思路,之前做过两个有序链表的合并操作,对于k个链表,我们最先想到的就是能不能转化为我们熟悉的两个链表的合并,可以我们先将k个链表分为两部分,先对这两部分完成链表的有序合并操作,再对合并得到的两个链表进行合并,这是一个递归性质的描述,采用递归很容易实现这个程序,和数组

[leetcode]Merge k Sorted Lists @ Python

原题地址:https://oj.leetcode.com/problems/merge-k-sorted-lists/ 题意:Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 解题思路:归并k个已经排好序的链表.使用堆这一数据结构,首先将每条链表的头节点进入堆中,然后将最小的弹出,并将最小的节点这条链表的下一个节点入堆,依次类推,最终形成的链表就是归

[LeetCode 题解]: Merge k Sorted Lists

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题意:对k个有序的链表进行归并排序.并分析其复杂度. /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(N

LeetCode 21 23:Merge Two Sorted Lists &amp; Merge K Sorted Lists

LeetCode 21: Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 题目分析:对两个有序列表进行合并,这个是数据结构基本题目比较简单,代码如下(这个代码有点长,可以简化): ListNode *mergeTwoL

Merge k Sorted Lists leetcode java

题目: Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 题解: Merge k sorted linked list就是merge 2 sorted linked list的变形题. 而且我们很自然的就想到了经典的Merge Sort,只不过那个是对数组进行sort.而不同的地方,仅仅是Merge两个list的操作不同. 这里来复习一下Merge