public ListNode mergeKLists(ArrayList<ListNode> lists) {
        if (lists == null || lists.size() == 0) {
            return null;
        }
        if (lists.size() == 1) {
            return lists.get(0);
        }

        ListNode head = new ListNode(0);
        ListNode result = head;
        while (true) {
            int nullCount = 0;
            ListNode min = null;
            int minValue = Integer.MAX_VALUE;
            int minI = 0;

            for (int i = 0; i < lists.size(); i++) {
                ListNode cur = lists.get(i);
                if (cur == null) {
                    nullCount++;
                } else {
                    if ( minValue > cur.val) {
                        minValue = cur.val;
                        min = cur;
                        minI = i;
                    }
                }
            }
            if (nullCount == lists.size()) {
                break;
            }
            head.next = min;
            head = head.next;
            lists.set(minI, min.next);

        }


        return result.next;
    }