<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>

class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        // Use a set to store pairs of (value, index) in sorted order
        set<pair<int, int>> nodeSet;

        // Initialize the set with the first node of each list (if not null)
        for (int i = 0; i < lists.size(); ++i) {
            if (lists[i]) {
                nodeSet.insert({lists[i]->val, i});
            }
        }

        // If the set is empty, all lists are empty, return nullptr
        if (nodeSet.empty()) {
            return nullptr;
        }

        // Create the result linked list with the smallest element from the set
        auto smallest = *nodeSet.begin();  // Get the smallest node
        int index = smallest.second;       // Get the index of that node
        ListNode* result = new ListNode(smallest.first);  // Create the first node
        ListNode* current = result;        // Pointer to the current node in the result list

        // Move to the next node in the corresponding list
        lists[index] = lists[index]->next;
        nodeSet.erase(nodeSet.begin());  // Remove the processed node from the set

        // If the list still has nodes, insert the next node into the set
        if (lists[index]) {
            nodeSet.insert({lists[index]->val, index});
        }

        // Continue merging the lists while the set has elements
        while (!nodeSet.empty()) {
            auto smallest = *nodeSet.begin();  // Get the smallest node
            int index = smallest.second;       // Get the index of that node

            // Create a new node in the result list with the smallest value
            current->next = new ListNode(smallest.first);
            current = current->next;           // Move to the newly added node

            // Move to the next node in the corresponding list
            lists[index] = lists[index]->next;
            nodeSet.erase(nodeSet.begin());    // Remove the processed node

            // If the list still has nodes, insert the next node into the set
            if (lists[index]) {
                nodeSet.insert({lists[index]->val, index});
            }
        }

        return result;  // Return the head of the merged list
    }
};

20300