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

struct CompareNode {
    bool operator()(ListNode* l1, ListNode* l2)
    {
        // return "true" if "p1" is ordered
        // before "p2", for example:
        return l1->val > l2->val;
    }
};

using namespace std; 
class Solution {
public:
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        
        // make a priority queue with index of the element too value,i,j
        // extract minimum element and put into the list
        // time? (n*m*log(n*m)) insertion time of each element
        // whichever index is removed eg v,i,j we will insert v,i,j+1

        // if(lists.size()<=0){
        //     return nullptr;
        // }
        // if(lists.size()==1){
        //     if(lists[0] == nullptr){
        //         return nullptr;
        //     }
        // }
        // std::cout<<"lists size: "<< lists.size()<<std::endl;
        priority_queue<ListNode*, vector<ListNode*>, CompareNode> pq;
        for(int itr=0; itr<lists.size(); itr++){
            if(lists[itr]!=nullptr){
                pq.push(lists[itr]);
            }
        }
        if(pq.size()==0){
            return nullptr;
        }

        ListNode* final_node = nullptr;
        ListNode* last_node = nullptr;
        // std::cout << final_node << std::endl;
        while(!pq.empty()){
            ListNode* minnode = pq.top();
            // std::cout<<"minnode at top: "<< minnode->val << std::endl;
            pq.pop();
            if(final_node != nullptr){
                // std::cout << "last node val: " << last_node->val << std::endl;
                // std::cout << "minnode val: " << minnode->val << std::endl;
                ListNode* new_node = new ListNode(minnode->val);
                // new_node->val = minnode->val;
                last_node->next = new_node;
                last_node = last_node->next;
            }
            else{
                final_node = minnode;
                last_node = minnode;
                // std::cout << "last node val: " << last_node->val << std::endl;
                // std::cout << "minnode val: " << minnode->val << std::endl;
            }
            
            if(minnode->next){
                pq.push(minnode->next);
            }
        }
        
        return final_node;

    }
};
19000