Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
// Start typing your Java solution below
// DO NOT write main() function
if(lists.size() == 0)
return null;
int start=0;
ListNode list1 = null;
while(list1==null && start<lists.size()){
list1 = lists.get(start);
start++;
}
for(int i=start; i<lists.size(); i++){
ListNode list2 = lists.get(i);
if(list2 == null)
continue;
ListNode mergedHead = new ListNode(-1);
ListNode mergedCur = mergedHead;
while(list1!=null && list2!=null){
if(list1.val <= list2.val){
mergedCur.next = list1;
mergedCur = mergedCur.next;
list1 = list1.next;
}
else{
mergedCur.next = list2;
mergedCur = mergedCur.next;
list2 = list2.next;
}
}
while(list1!=null){
mergedCur.next = list1;
mergedCur = mergedCur.next;
list1 = list1.next;
}
while(list2!=null){
mergedCur.next = list2;
mergedCur = mergedCur.next;
list2 = list2.next;
}
list1 = mergedHead.next;
}
return list1;
}
}
This algorithm merge need to do k-1 merge. Better solution should merge a pair of list each time and do it for lgk times.
The time complexity is O(k*lgN), Space O(N).
#PriorityQueue Sulotion#
public class Solution {
class NodeComparator implements Comparator<ListNode>{
public int compare(ListNode o1, ListNode o2){
return o1.val-o2.val;
};
}
public ListNode mergeKLists(ArrayList<ListNode> lists) {
// Start typing your Java solution below
// DO NOT write main() function
if(lists.size() == 0)
return null;
int size = lists.size();
ListNode head = new ListNode(0); // dummy node
ListNode cur = head;
PriorityQueue<ListNode> pq =
new PriorityQueue<ListNode>(size, new NodeComparator());
for(int i=0; i<size; i++){
ListNode node = lists.get(i);
if(node==null)
continue;
pq.add(node);
}
while(pq.size()>0){
// dequeue --- get the min Node
ListNode next = pq.poll();
cur.next = next;
cur = next;
// enqueue from the same list
if(next.next!=null)
pq.add(next.next);
}
head = head.next;
return head;
}
}
Time Complexity: O(K*lgN) k=num of list, N=total number of Nodes.
Space:O(k).
------------------------------------------------------------------------------------------------------------------------------------------
LL's solution:
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) {
* val = x;
* next = null;
* }
* }
*/
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
// Start typing your Java solution below
// DO NOT write main() function
// remove all the empty listnode
ListNode empty = null;
while(lists.contains(empty))
lists.remove(empty);
int len = lists.size();
if(len==0)
return null;
if(len == 1 )
return lists.get(0);
ListNode dummy = new ListNode(0);
ListNode runner = dummy;
while(lists.size()>1){
// find min head
int min = lists.get(0).val;
int min_index = 0;
for(int i = 1; i<lists.size(); i++){
ListNode head = lists.get(i);
if(head.val<min){
min = head.val;
min_index = i;
}
}
// add min head to runner
runner.next = lists.get(min_index);
runner = runner.next;
if(lists.get(min_index).next!=null)
lists.set(min_index,lists.get(min_index).next);
else
lists.remove(min_index);
}
runner.next = lists.get(0);
return dummy.next;
}
}
每次遍历list里的head,找出最小,连在res后面,head后移。
Note:list.get 返回的是copy,所以必须把list.get(i).next的值重新赋给list(i), using list.set(i,new_value).