lambda函数实现链表的小根堆

struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    explicit ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};
int main()
{
    auto cmp = [](ListNode* L1, ListNode* L2){
        return L1->val > L2 -> val;
    };
    priority_queue<ListNode*, vector<ListNode*>,
            decltype(cmp)> q(cmp);
    auto head = new ListNode(2);
    head->next = new ListNode(1);
    head->next->next= new ListNode(3);
    head->next->next->next = new ListNode(1);
    q.push(head);
    cout<<q.top()->val<<endl;
    q.push(head->next);
    cout<<q.top()->val<<endl;

    return 0;
}
posted @ 2021-08-26 17:09  蘑菇王国大聪明  阅读(69)  评论(0编辑  收藏  举报