合并k个有序链表 - lifengyu360/lifengyu_first_git_test GitHub Wiki

class Solution {

public:

struct Status {

    int val;
    ListNode *ptr;
    bool operator < (const Status &rhs) const {
        return val > rhs.val;
    }
};

priority_queue <Status> q;

ListNode* mergeKLists(vector<ListNode*>& lists) {
    for (auto node: lists) {
        if (node) q.push({node->val, node});
    }
    ListNode head, *tail = &head;
    while (!q.empty()) {
        auto f = q.top(); q.pop();
        tail->next = f.ptr; 
        tail = tail->next;
        if (f.ptr->next) q.push({f.ptr->next->val, f.ptr->next});
    }
    return head.next;
}

};

⚠️ **GitHub.com Fallback** ⚠️