Leetcode–Merge k Sorted Lists

The Problem:

Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity.

Notes:

  1. First straightforward solution: just like doing merge two sorted list. Examine head node of each list, add the min one to result, complexity is O(k*k*n), k is number of lists, n is number of element in each list.
  2. A better solution is to maintain a priority queue of k candidates from each list, each iteration update the queue and get the min number, no need to traverse k numbers again to get min, complexity is O(k*n).

Java Solution I

/**
 * 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(List<ListNode> lists) {
        int k = lists.size();
        if(k==0) return null;
        ListNode[] pointers = new ListNode[k];
        for(int i = 0; i < k; i++){
            pointers[i] = lists.get(i);
        }
        ListNode fkHead = new ListNode(0);
        ListNode pre = fkHead;
        while(true){
            int min = Integer.MAX_VALUE;
            int useindex = -1;
            for(int i = 0; i < k; i++){
                if(pointers[i] != null && pointers[i].val < min){
                    min = pointers[i].val;
                    useindex = i;
                }
            }
            if(useindex==-1)
                break;
            pre.next = pointers[useindex];
            pre = pre.next;
            pointers[useindex] = pointers[useindex].next;
        }
        pre.next = null;
        return fkHead.next;
    }
}

Java Solution II

/**
 * 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(List<ListNode> lists) {
        int k = lists.size();
        if(k==0) return null;
        PriorityQueue<ListNode> queue = new PriorityQueue(k, new NodeComp());

        for(int i = 0; i < k; i++){
            if(lists.get(i)!= null){
                queue.add(lists.get(i));   
            }
        }
        ListNode fkHead = new ListNode(0);
        ListNode pre = fkHead;
        while(true){
            if(queue.size()==0)
                break;
            ListNode min = queue.poll();
            pre.next = min;
            pre = pre.next;
            if(min.next != null)
                queue.add(min.next);
        }
        pre.next = null;
        return fkHead.next;
    }
    
    
}
class NodeComp implements Comparator<ListNode>{
    public int compare(ListNode a, ListNode b){
        return a.val-b.val;
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s