LeetCode – 23. Merge k Sorted Lists

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;


}


/*

*/

转载于:https://www.cnblogs.com/crazyacking/p/5200073.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/109148.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Intellij IDEA 最全实用快捷键整理(长期更新)[通俗易懂]

    Intellij IDEA 最全实用快捷键整理(长期更新)[通俗易懂]IntellijIDEA最全实用快捷键整理(长期更新)正文前:1.IDEA内存优化(秒开的快感!!)因机器本身的配置而配置:\IntelliJIDEA8\bin\idea.exe.vmoptions//(根据你的配置变大!!)——————————————Xms2048m-Xmx2048m-XX:MaxPermSize=512m…

    2022年5月12日
    78
  • CentOS镜像下载(阿里云源)[通俗易懂]

    CentOS镜像下载(阿里云源)[通俗易懂]文章目录1.下载链接2.下载步骤3.版本说明1.下载链接CentOS7.9.2009:http://mirrors.aliyun.com/centos/7.9.2009/isos/x86_64/所有版本:http://mirrors.aliyun.com/centos/官网下载页:https://www.centos.org/download/2.下载步骤进入阿里云CentOS7.9.2009下载页,点击CentOS-7-x86_64-DVD-2009.iso以进行下载

    2022年5月6日
    50
  • c++ sstream

    c++ sstreamsstream定义了三个类:istringstream、ostringstream和stringstream分别用来进行流的输入、输出和输入输出操作由于sstream使用string对象代替字符数组,避免缓冲区溢出的危险;其次,因为传入参数和目标对象的类型会被自动推导出来,所以不存在错误的格式化符的问题。相比c库的数据类型转换,sstream更加安全、自动和直接。1.数据类型转换#inclu…

    2022年6月4日
    49
  • Oracle 中的视图理解

    Oracle 中的视图理解

    2021年8月19日
    64
  • getComputedStyle()与currentStyle()、style()方法「建议收藏」

    JS使用getComputedStyle()方法获取CSS属性值 在对网页进行调试的过程中,经常会用到js来获取元素的CSS样式,方法有很多很多,现在仅把我经常用的方法总结如下:1.obj.style:这个方法只能JS只能获取写在html标签中的写在style属性中的值(style=”…”),而无法获取定义在&lt;styletype="text/c…

    2022年4月9日
    47
  • matlab维纳滤波器函数_逆滤波器

    matlab维纳滤波器函数_逆滤波器[Matlab]维纳滤波器设计​ 维纳滤波(wienerfiltering)一种基于最小均方误差准则、对平稳过程的最优估计器。这种滤波器的输出与期望输出之间的均方误差为最小,因此,它是一个最佳滤波系统。它可用于提取被平稳噪声污染的信号。​ 从连续的(或离散的)输入数据中滤除噪声和干扰以提取有用信息的过程称为滤波,这是信号处理中经常采用的主要方法之一,具有十分重要的应用价值,而相应的装置称为…

    2022年10月22日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号