<p>You are given an array of <code>k</code> linked-lists <code>lists</code>, each linked-list is sorted in ascending order.</p>

<p><em>Merge all the linked-lists into one sorted linked-list and return it.</em></p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> lists = [[1,4,5],[1,3,4],[2,6]]
<strong>Output:</strong> [1,1,2,3,4,4,5,6]
<strong>Explanation:</strong> The linked-lists are:
[
  1-&gt;4-&gt;5,
  1-&gt;3-&gt;4,
  2-&gt;6
]
merging them into one sorted list:
1-&gt;1-&gt;2-&gt;3-&gt;4-&gt;4-&gt;5-&gt;6
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> lists = []
<strong>Output:</strong> []
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> lists = [[]]
<strong>Output:</strong> []
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>k == lists.length</code></li>
	<li><code>0 &lt;= k &lt;= 10<sup>4</sup></code></li>
	<li><code>0 &lt;= lists[i].length &lt;= 500</code></li>
	<li><code>-10<sup>4</sup> &lt;= lists[i][j] &lt;= 10<sup>4</sup></code></li>
	<li><code>lists[i]</code> is sorted in <strong>ascending order</strong>.</li>
	<li>The sum of <code>lists[i].length</code> will not exceed <code>10<sup>4</sup></code>.</li>
</ul>

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        set<int> allOccs;
        unordered_map<int, ListNode*> m;
        ListNode* dummy = new ListNode(0);
        int i;
        for(i = 0; i < lists.size(); i++){
            if(lists[i] != NULL){
                ListNode* curr = new ListNode(lists[i] -> val);
                allOccs.insert(lists[i] -> val);
                m[lists[i] -> val] = curr;
                dummy -> next = curr;
                ListNode* iter = lists[i] -> next;
                while(iter != NULL){
                    curr -> next = new ListNode(iter -> val);
                    allOccs.insert(iter -> val);
                    m[iter -> val] = curr -> next;
                    curr = curr -> next;
                    iter = iter -> next;
                }
                break;
            }
        }

        for(i = i + 1; i < lists.size(); i++){
            ListNode* iter = lists[i];
            while(iter != NULL){
                auto pos = lower_bound(allOccs.begin(), allOccs.end(), iter -> val);
                if(pos == allOccs.begin()){
                    cout << iter -> val << " " << *pos << endl;
                    ListNode* curr = new ListNode(iter -> val);
                    curr -> next = dummy -> next;
                    dummy -> next = curr;
                    allOccs.insert(iter -> val);
                    if(curr -> next == NULL || curr -> val != curr -> next -> val){
                        m[curr -> val] = curr;
                    }
                } else {
                    pos--;
                    ListNode* temp = new ListNode(iter -> val);
                    cout << iter -> val << " " << *pos << endl;
                    temp -> next = m[*pos] -> next;
                    m[*pos] -> next = temp;
                    if(temp -> next == NULL || temp -> val != temp -> next -> val){
                        m[temp -> val] = temp;
                    }
                    allOccs.insert(iter -> val);
                }
                iter = iter -> next;
            }
        }
        return dummy -> next;
    }
};
19500