Leetcode 23. Merge k Sorted Lists

23. Merge k Sorted Lists

  • Total Accepted: 92690
  • Total Submissions:385821
  • Difficulty: Hard

 

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

 

思路:

可以先做21. Merge Two Sorted Lists

 

方法一:直接按定义做,复杂度和下面的方法差不多。

方法二:每次递归归并lists中前2条列表,直到归并只剩1条列表。

方法三:首尾归并,每次归并后,lists规模减少一半。

 

 

注意,局部函数中的变量一般要先初始化!

 

代码:

方法一:

 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* head=new ListNode(-1);
13         ListNode* end=head;
14         int i,num=0,k=lists.size();
15         long long minnum=-1;
16         while(minnum!=LONG_MAX){
17             minnum=LONG_MAX;
18             for(i=0;i<k;i++){
19                 if(!lists[i]) continue;
20                 if(minnum>lists[i]->val){
21                     minnum=lists[i]->val;
22                 }
23             }
24             for(i=0;i<k;i++){
25                 if(lists[i]&&minnum==lists[i]->val){
26                     end->next=new ListNode(minnum);
27                     end=end->next;
28                     lists[i]=lists[i]->next;
29                 }
30             }
31         }
32         return head->next;
33     }
34 };

 

方法二:

 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* mergeTwoLists(ListNode* a,ListNode* b){
12         if(!a){
13             return b;
14         }
15         if(!b){
16             return a;
17         }
18         if(a->val>b->val){
19             b->next=mergeTwoLists(a,b->next);
20             return b;
21         }
22         a->next=mergeTwoLists(a->next,b);
23         return a;
24     }
25     ListNode* mergeKLists(vector<ListNode*>& lists) {
26         ListNode* head=NULL;//一定要初始化!!
27         while(lists.size()>1){
28             lists.push_back(mergeTwoLists(lists[0],lists[1]));
29             lists.erase(lists.begin());
30             lists.erase(lists.begin());
31         }
32         if(lists.size()){
33             head=lists[0];
34         }
35         return head;
36     }
37 };

 

 

方法三:

 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* mergeTwoLists(ListNode* a,ListNode* b){
12         if(!a){
13             return b;
14         }
15         if(!b){
16             return a;
17         }
18         if(a->val>b->val){
19             b->next=mergeTwoLists(a,b->next);
20             return b;
21         }
22         a->next=mergeTwoLists(a->next,b);
23         return a;
24     }
25     ListNode* mergeKLists(vector<ListNode*>& lists) {
26         if(lists.empty()){
27             return NULL;
28         }
29         int len=lists.size();
30         int b=0,e=len-1;
31         while(b<e){
32             while(b<e){
33                lists[b]=mergeTwoLists(lists[b],lists[e]);
34                b++;
35                e--;
36             }
37             b=0;
38         }
39         return lists[0];
40     }
41 };

 

或者

 

 1 class Solution {
 2 public:
 3     ListNode* mergeTwoLists(ListNode* a,ListNode* b){
 4         if(!a){
 5             return b;
 6         }
 7         if(!b){
 8             return a;
 9         }
10         if(a->val>b->val){
11             b->next=mergeTwoLists(a,b->next);
12             return b;
13         }
14         a->next=mergeTwoLists(a->next,b);
15         return a;
16     }
17     ListNode* mergeKLists(vector<ListNode*>& lists) {
18         vector<ListNode*> res;
19         int i;
20         for(i=0;i<lists.size();i++){
21             if(lists[i]){
22                 res.push_back(lists[i]);
23             }
24         }
25         if(!res.size()){
26             return NULL;
27         }
28         int len=res.size();
29         while(len>1){
30             //设len=2q或者2q+1
31             for(i=0;i<len/2;i++){//注意区分len是奇数还是偶数的情况
32                 res[i]=mergeTwoLists(res[i],res[len-1-i]);
33             }
34             //此时len应该等于q(len为偶数)或者q+1(len为奇数)
35             len=(len+1)/2;
36         }
37         return res[0];
38     }
39 };

 

 

posted @ 2016-07-12 22:37  Deribs4  阅读(245)  评论(0编辑  收藏  举报