admin管理员组

文章数量:1623789

重写greater函数

Leetcode:23 合并k个排序链表


重载函数使用结构体实现!!

struct cmp{  //对新的数据类型的 < 进行重写
    bool operator()(ListNode *a,ListNode *b){
        return a->val > b->val;
    }
};
priority_queue<ListNode*, vector<ListNode*>, cmp> heap; // 小根堆

完整代码:

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    struct cmp{  //对新的数据类型的<进行重写
       bool operator()(ListNode *a,ListNode *b){
          return a->val > b->val;
       }
    };
    ListNode* mergeKLists(vector<ListNode*>& lists) {
        pr

本文标签: 重写函数链表大小greater