Java iterator
import java.util.*;
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
if(lists==null||lists.size()==0) return null;
Iterator it = lists.iterator();
ListNode res = (ListNode)it.next();
ListNode cur = null;
while(it.hasNext()){
cur = (ListNode)it.next();
res = mergeList(res, cur);
}
return res;
}
private ListNode mergeList(ListNode l1, ListNode l2){
ListNode head = new ListNode(0);
ListNode tail = head;
while(l1!=null&&l2!=null){
if(l1.val>l2.val){
tail.next = l2;
l2 = l2.next;
}else{
tail.next = l1;
l1 = l1.next;
}
tail = tail.next;
}
if(l1==null) tail.next = l2;
else tail.next = l1;
return head.next;
}
}


京公网安备 11010502036488号