Simple and concise JAVA solution


  • 0
    J
    class Solution 
    {
        public ListNode mergeKLists(ListNode[] lists) 
        {
            Queue<ListNode> pq = new PriorityQueue<>((ListNode n1, ListNode n2) -> n1.val - n2.val);
            for (ListNode listNode : lists)
            {
                if (listNode != null) pq.add(listNode);
            }
            ListNode dummy = new ListNode(0);
            ListNode tail = dummy;
            while (!pq.isEmpty())
            {
                ListNode node = pq.poll();
                tail.next = node;
                tail = tail.next;
                if (node.next != null) pq.offer(node.next);
            }
            return dummy.next;
        }
    }
    

Log in to reply
 

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