LeetCode OJ:Merge k Sorted Lists(归并k个链表)

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

类似于归并2个链表,暴力一点的方法就是,每取出一个list就与以前的list归并返回merge后list,知道所有list merge完成。

但是可惜,这样做会TLE。贴下代码先:

 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) {
12         ListNode * ret = NULL;
13         for(auto * it : lists){
14             ret = mergeList(ret, it);
15         }
16         return ret;
17     }
18
19     ListNode * mergeList(ListNode * head1, ListNode * head2)
20     {
21         if(!head1) return head2;
22         if(!head2) return head1;
23         ListNode * head = new ListNode(-1);
24         head->next = head1;
25         ListNode * prev = head;
26         ListNode * tmpNode = NULL;
27         while(head1 && head2){
28             if(head1->val < head2->val){
29                 prev = prev->next;
30                 head1 = head1->next;
31             }else{
32                 tmpNode = head2->next;
33                 prev->next = head2;
34                 head2->next = head1;
35                 prev = head2;
36                 head2 = tmpNode;
37             }
38         }
39         if(head2){
40             prev->next = head2;
41         }
42         return head->next;
43     }
44 };

下面是用堆来做的,先建立一个小堆,找到做小元素。将其merge到一个链表里面。如果后面还有元素,再将其放到堆中。代码如下:

 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     static bool Comp(ListNode * l1, ListNode * l2)
12     {
13         if(l1->val < l2->val)
14             return false;
15         return true;
16     }
17
18     ListNode* mergeKLists(vector<ListNode*>& lists) {
19         vector<ListNode *> removeNullList;
20         removeNullList.reserve(lists.size());
21         for(auto it = lists.begin(); it != lists.end(); ++it){
22             if(*it != NULL)
23                 removeNullList.push_back(*it);
24         }
25         if(removeNullList.size() == 0) return NULL;    //首先去除Null节点
26         make_heap(removeNullList.begin(), removeNullList.end(), Comp);
27         ListNode * helper = new ListNode(-1);
28         ListNode * tail = helper;
29         ListNode * minNode = NULL;
30         while(!removeNullList.empty()){
31             pop_heap(removeNullList.begin(), removeNullList.end(), Comp);
32             minNode = removeNullList[removeNullList.size()-1];
33             removeNullList.pop_back();
34             tail->next = minNode;
35             tail = tail->next;
36             if(minNode->next){//如果后面还有list的话,再次入堆
37                 removeNullList.push_back(minNode->next);
38                 make_heap(removeNullList.begin(), removeNullList.end(), Comp);
39             }
40         }
41         return helper->next;
42     }
43 };
时间: 2024-10-24 23:54:48

LeetCode OJ:Merge k Sorted Lists(归并k个链表)的相关文章

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. 翻译: 把2个有序链表连接,返回一个新链表 思路: 很简单,就是遍历每个节点,小的话添加在新链表的后面.如果一个为空,则把另一个接在新链表结尾. 第二种思路就是以一个链表为基准,然后和另一个比较,如果l2的节

[LeetCode] 23. Merge k Sorted Lists 合并k个有序链表

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 与21. Merge Two Sorted Lists的拓展,这道题要合并k个有序链表,还是可以两两合并. 类似题目: [LeetCode] 21. Merge Two Sorted Lists 合并有序链表 原文地址:https://www.cnblogs.com/lightwindy/p/8512

LeetCode:Merge Two Sorted Lists - 拼接两个有序链表

1.题目名称 Merge Two Sorted Lists(按升序拼接两个有序链表) 2.题目地址 https://leetcode.com/problems/merge-two-sorted-lists/ 3.题目内容 英文: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 l

[LeetCode]21 Merge Two Sorted Lists 合并两个有序链表

---恢复内容开始--- [LeetCode]21 Merge Two Sorted Lists 合并两个有序链表 Description 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. Example Example: Input: 1->2->4, 1-&g

Leetcode 23.Merge Two Sorted Lists Merge K Sorted Lists

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. 依次拼接 复杂度 时间 O(N) 空间 O(1) 思路 该题就是简单的把两个链表的节点拼接起来,我们可以用一个Dummy头,将比较过后的节点接在这个Dummy头之后.最后

leetCode 23. Merge k Sorted Lists (合并k个排序链表) 解题思路和方法

Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:此题是由合并两个排序链表演化而来,刚开始,想法比较简单,像求最大公共前缀一样,逐一求解:但是最后超时,所以马上意识到出题方是为了使用归并和分治的方法,故重新写了代码. 代码一(超时未过): /** * Definition for singly-link

[Leetcode] Merge k sorted lists 合并k个已排序的链表

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思路:这题最容易想到的是,(假设有k个链表)链表1.2合并,然后其结果12和3合并,以此类推,最后是123--k-1和k合并.至于两链表合并的过程见merge two sorted lists的分析.复杂度的分析见JustDoIT的博客.算法复杂度:假设每个链表的平均长度是n,则1.2合并,遍历2n个

[LeetCode]148.Merge Two Sorted Lists

[题目] Sort a linked list in O(n log n) time using constant space complexity. [分析] 单链表适合用归并排序,双向链表适合用快速排序.本题可以复用Merge Two Sorted Lists方法 [代码] /********************************* * 日期:2015-01-12 * 作者:SJF0115 * 题目: 148.Merge Two Sorted Lists * 来源:https://

[Leetcode][JAVA] Merge Two Sorted Lists &amp; Sort List

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. 两个排好序的链表拼接,只要用两个指针遍历链表即可. 可以借助一个helper指针来进行开头的合并.如果有一个遍历完,则直接把另一个链表指针之后的部分全部接在后面: 1

leetCode 21.Merge Two Sorted Lists (合并排序链表) 解题思路和方法

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. 思路:对两个已排序的单链表合并.算法上比较简单,与归并排序类似.只是数据结构上以前学的,现在忘的比较多,写第一遍的时候比较费力.而且想把重复代码写出方法,但是方法怎么都不