Simple java sol using PriorityQueue


  • 5
    C
    public class Solution {
        public ListNode mergeKLists(ListNode[] lists) {
    
            Queue<ListNode> q = new PriorityQueue<ListNode>(new ListComparator());
            for(ListNode n: lists){
                if( n!= null) {
                    q.add(n);
                }
            }
            ListNode head = new ListNode(0), p = head, cur = null;
            while( !q.isEmpty()) {
                cur = q.poll();
                if(cur.next!=null)
                    q.offer(cur.next);
                p.next = cur;
                p = p.next;
            }
            return head.next;
        }
    }
    
    class ListComparator implements Comparator<ListNode>{
    
    	@Override
    	public int compare(ListNode n1, ListNode n2) {
    		return n1.val - n2.val;
    	}
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.