package leetcode;

/**
 * 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) {
        // TODO
        return null;
    }

    private static void print(ListNode node) {
        for (ListNode n = node; n != null; n = n.next) {
            System.out.print(node.val + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        Problem23 prob = new Problem23();

        ListNode list1 = new ListNode(1);
        list1.next = new ListNode(3);
        list1.next.next = new ListNode(5);

        ListNode list2 = new ListNode(2);
        list2.next = new ListNode(7);
        list2.next.next = new ListNode(9);
        list2.next.next.next = new ListNode(10);
        list2.next.next.next.next = new ListNode(12);

        ListNode list3 = new ListNode(4);
        list3.next = new ListNode(6);
        list3.next.next = new ListNode(8);
        list3.next.next.next = new ListNode(11);

        ListNode result = prob.mergeKLists(new ListNode[]{list1, list2, list3});
        print(result);
    }
}

@@ -304,35 +304,1861 @@
     
-// TODO%0A        return null
+ListNode result = null;%0A        for (ListNode n : lists) %7B%0A            if (result == null) %7B%0A                result = n;%0A            %7D else %7B%0A                result = merge(result, n);%0A            %7D%0A        %7D%0A        return result;%0A    %7D%0A%0A    private ListNode merge(ListNode node1, ListNode node2) %7B%0A        ListNode n1 = node1;%0A        ListNode n2 = node2;%0A        ListNode resultHead = null;%0A        ListNode result = null;%0A        while (true) %7B%0A            while (n1 != null && n2 != null && n1.val %3C= n2.val) %7B%0A                if (result == null) %7B%0A                    result = n1;%0A                    resultHead = result;%0A                %7D else %7B%0A                    result.next = n1;%0A                    result = result.next;%0A                %7D%0A                n1 = n1.next;%0A            %7D%0A            if (n1 == null) %7B%0A                break;%0A            %7D%0A            while (n1 != null && n2 != null && n1.val %3E n2.val) %7B%0A                if (result == null) %7B%0A                    result = n2;%0A                    resultHead = result;%0A                %7D else %7B%0A                    result.next = n2;%0A                    result = result.next;%0A                %7D%0A                n2 = n2.next;%0A            %7D%0A            if (n2 == null) %7B%0A                break;%0A            %7D%0A        %7D%0A        while (n1 != null) %7B%0A            if (result == null) %7B%0A                result = n1;%0A                resultHead = result;%0A            %7D else %7B%0A                result.next = n1;%0A                result = result.next;%0A            %7D%0A            n1 = n1.next;%0A        %7D%0A        while (n2 != null) %7B%0A            if (result == null) %7B%0A                result = n2;%0A                resultHead = result;%0A            %7D else %7B%0A                result.next = n2;%0A                result = result.next;%0A            %7D%0A            n2 = n2.next;%0A        %7D%0A        return resultHead
 ;%0A  
@@ -2296,19 +2296,16 @@
 .print(n
-ode
 .val + %22
@@ -2964,24 +2964,24 @@
 tNode(11);%0A%0A
-
         List
@@ -3071,16 +3071,111 @@
 result);
+%0A%0A        result = prob.mergeKLists(new ListNode%5B%5D%7Blist1, null, list3%7D);%0A        print(result);
 %0A    %7D%0A%7D

Update problem 23