LeetCode - 23. Merge k Sorted Lists

23. Merge k Sorted Lists #

Problem's Link
#

 ----------------------------------------------------------------------------

Mean: 

将k个有序链表合并为一个有序链表.

analyse:

方法一:本着不重复发明轮子的原则,使用两两合并,就可以利用前面已经做过的Merge two Sorted Lists.

方法二:抖机灵.将所有结点的val都push_back在一个vector容器中,排序后又重新构造链表.

Time complexity: O(N)

 

view code

方法一:

ListNode *mergeKLists(vector<ListNode *> &lists) {
   if(lists.empty()){
       return nullptr;
   }
   while(lists.size() > 1){
       lists.push_back(mergeTwoLists(lists[0], lists[1]));
       lists.erase(lists.begin());
       lists.erase(lists.begin());
   }
   return lists.front();
}
ListNode *mergeTwoLists(ListNode *l1, ListNode *l2) {
   if(l1 == nullptr){
       return l2;
   }
   if(l2 == nullptr){
       return l1;
   }
   if(l1->val <= l2->val){
       l1->next = mergeTwoLists(l1->next, l2);
       return l1;
   }
   else{
       l2->next = mergeTwoLists(l1, l2->next);
       return l2;
   }
}

方法二:

/**
* -----------------------------------------------------------------
* Copyright (c) 2016 crazyacking.All rights reserved.
* -----------------------------------------------------------------
*       Author: crazyacking
*       Date  : 2016-02-18-09.02
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
using namespace std;
typedef long long(LL);
typedef unsigned long long(ULL);
const double eps(1e-8);

// 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)
   {
       vector<int> nodeVal;
       for(auto ptr:lists)
       {
           while(ptr)
           {
               nodeVal.push_back(ptr->val);
               ptr=ptr->next;
           }
       }
       if(nodeVal.size()<=0)
       {
           return nullptr;
       }

       sort(nodeVal.begin(),nodeVal.end());
       bool isFirst=1;
       ListNode *res=nullptr,*head=nullptr;
       for(auto p:nodeVal)
       {
           if(isFirst)
           {
               isFirst=0;
               head=new ListNode(p);
               res=head;
           }
           else
           {
               head->next=new ListNode(p);
               head=head->next;
           }
       }
       return res;
   }
};

ListNode* getList(vector<int>& arr)
{
   bool isFirst=1;
   ListNode *res=nullptr,*head=nullptr;
   for(auto p:arr)
   {
       if(isFirst)
       {
           isFirst=0;
           head=new ListNode(p);
           res=head;
       }
       else
       {
           head->next=new ListNode(p);
           head=head->next;
       }
   }
   return res;
}

int main()
{
   Solution solution;
   int n;
   while(cin>>n)
   {
       vector<ListNode*> listVe;
       while(n--)
       {
           int num;
           cin>>num;
           vector<int> ve;
           for(int i=0;i<num;++i)
           {
               int tmp;
               cin>>tmp;
               ve.push_back(tmp);
           }
           listVe.push_back(getList(ve));
       }
       ListNode* head=solution.mergeKLists(listVe);
       while(head)
       {
           cout<<head->val<<" ";
           head=head->next;
       }
       cout<<"End."<<endl;
   }
   return 0;
}
/*

*/
posted @   北岛知寒  阅读(201)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
点击右上角即可分享
微信分享提示
主题色彩