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
ListNode rst = new ListNode(-1001);
if(lists.size()==0){
return null;
}
rst.next = lists.get(0);
for(int i = 1; i < lists.size(); i++){
rst = addNode(rst,lists.get(i));
}
return rst.next;
}
public ListNode addNode(ListNode rst,ListNode tgt){
ListNode now = new ListNode(-1001);
now.next = tgt;
ListNode node;
ListNode nur = new ListNode(-1001);
nur = rst;
while(now.next!=null){
node = now.next;
now.next = node.next;
node.next = null;
while(nur.next!=null && node.val > nur.next.val){
nur = nur.next;
}
if(nur.next!=null){
node.next = nur.next;
nur.next = node;
}else{
nur.next =node;
node.next = now.next;
break;
}
}
return rst;
}
}