package leetcode;

import java.util.PriorityQueue;

/**
 * https://leetcode.com/problems/merge-k-sorted-lists/
 */
public class Problem23 {
    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    public ListNode mergeKLists(ListNode[] lists) {
        PriorityQueue<ListNode> queue = new PriorityQueue<>((a, b) -> Integer.compare(a.val, b.val));
        for (ListNode node : lists) {
            for (ListNode n = node; n != null; n = n.next) {
                queue.add(n);
            }
        }
        ListNode result = null;
        ListNode resultHead = null;
        while (!queue.isEmpty()) {
            ListNode n = queue.remove();
            n.next = null;
            if (result == null) {
                result = n;
                resultHead = result;
            } else {
                result.next = n;
                result = result.next;
            }
        }
        return resultHead;
    }
}

@@ -12,16 +12,45 @@
 tcode;%0A%0A
+import java.util.Comparator;%0A
 import j
@@ -418,42 +418,38 @@
 e%3C%3E(
-(a, b) -%3E Integer.compare(a.val, b
+Comparator.comparingInt(a -%3E a
 .val
@@ -626,22 +626,22 @@
 ode 
-result
+answer
  = null;
 %0A   
@@ -628,32 +628,44 @@
 e answer = null;
+ // the head
 %0A        ListNod
@@ -666,26 +666,20 @@
 istNode 
-resultHead
+node
  = null;
@@ -773,32 +773,59 @@
   n.next = null;
+ // null out to avoid cycle
 %0A            if 
@@ -825,22 +825,22 @@
     if (
-result
+answer
  == null
@@ -859,22 +859,22 @@
         
-result
+answer
  = n;%0A  
@@ -891,27 +891,16 @@
     
-resultHead = result
+node = n
 ;%0A  
@@ -934,22 +934,20 @@
         
-result
+node
 .next = 
@@ -969,23 +969,19 @@
     
-result = result
+node = node
 .nex
@@ -1026,18 +1026,14 @@
 urn 
-resultHead
+answer
 ;%0A  

Update problem 23