您当前的位置: 首页 >  链表

对方正在debug

暂无认证

  • 3浏览

    0关注

    399博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

合并K个排序链表(链表类优队)

对方正在debug 发布时间:2020-02-14 16:02:17 ,浏览量:3

题目:https://leetcode-cn.com/problems/merge-k-sorted-lists/submissions/

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode(int x) : val(x), next(NULL) {}
 * };
 */
class Solution {
public:
    struct cmp{//优队使用配套cmp
    	bool operator () (const ListNode *a,const ListNode *b) {
    		return a->val > b->val;
		}
	};
    ListNode* mergeKLists(vector& lists) {
        /*
        *优队链表
        *将有序链表都丢进队列,每次取最小的出来即可
        *
        */
        priority_queue q;//链表类优队
        int n = lists.size();
        for(int i = 0;i next = new ListNode(cur->val);
            p = p->next;
            if(cur->next != NULL) q.push(cur->next);
        }
        return ans->next;
    }
    
};
关注
打赏
1664895754
查看更多评论
立即登录/注册

微信扫码登录

0.2402s