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;
}
主要是给自己看的,所以肯定会出现很多错误哈哈哈哈哈