import java.util.*;
/*
* public class ListNode {
* int val;
* ListNode next = null;
* public ListNode(int val) {
* this.val = val;
* }
* }
*/
public class Solution {
/**
* 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
*
*
* @param lists ListNode类ArrayList
* @return ListNode类
*/
public ListNode mergeKLists (ArrayList<ListNode> lists) {
// write code here
// if(lists.size()==0){
// ListNode tmp = null;
// return tmp;
// }
ArrayList<Integer> vallist=new ArrayList<>();
//从所有list中拿到值
for(ListNode head:lists)
{
if(head==null) continue;
ListNode cur=head;
while(cur!=null)
{
vallist.add(cur.val);
cur=cur.next;
}
}
Collections.sort(vallist);//排序
//排除特殊情况,返回空链表
if(vallist.size()==0) {
ListNode tmp = null;
return tmp;
}
//构造返回结果
ListNode rst=new ListNode(vallist.get(0));
ListNode cur=rst;
for(int i=1;i<vallist.size();i++)
{
ListNode tmp = new ListNode(vallist.get(i));
cur.next=tmp;
cur=cur.next;
}
return rst;
}
}