If you can not edit the ListNode struct, you also can custom your own compare method of priority_queue
. Just do something like this:
struct cmp {
bool operator()(const ListNode* a, const ListNode* b) const {
return a->val > b->val;
}
};
When create a priority queue, you can use the struct of 'cmp' for the priority queue.
priority_queue<ListNode*, vector<ListNode*>, cmp> q;