当前位置:网站首页>[offer78] merge multiple ordered linked lists
[offer78] merge multiple ordered linked lists
2022-07-06 12:24:00 【Vigorous waist Nuo dance】
Merge multiple ordered linked lists
subject
Given a linked list array , Each list has been listed in ascending order .
Please merge all linked lists into one ascending linked list , Return the merged linked list .
Example 1:
Input :lists = [[1,4,5],[1,3,4],[2,6]]
Output :[1,1,2,3,4,4,5,6]
explain : The linked list array is as follows :
[
1->4->5,
1->3->4,
2->6
]
Combine them into an ordered list to get .
1->1->2->3->4->4->5->6
Example 2:
Input :lists = []
Output :[]
Example 3:
Input :lists = [[]]
Output :[]
Tips :
k == lists.length
0 <= k <= 10^4
0 <= lists[i].length <= 500
-10^4 <= lists[i][j] <= 10^4
lists[i] Press Ascending array
lists[i].length The sum of is not more than 10^4
source : Power button (LeetCode)
link :https://leetcode.cn/problems/vvXgSW
Copyright belongs to the network . For commercial reprint, please contact the official authority , Non-commercial reprint please indicate the source .
/* Method : Priority queue */
#include <vector>
#include<queue>
#include<iostream>
using namespace std;
struct ListNode {
int val;
ListNode* next;
ListNode() : val(0), next(nullptr) {
}
ListNode(int x) : val(x), next(nullptr) {
}
ListNode(int x, ListNode* next) : val(x), next(next) {
}
};
/* Preliminary knowledge : Function object 、 Class template 、 Anonymous object 、 The function of single chain header node 、priorty_queue Basic operation */
/* One . Priority queue (priority_queue) Initialization of requires the use of function objects , The function is to set how to judge the priority size of two elements in the heap , By default less<T>()*/
/* Two . Something that can act as a function object : Ordinary function 、 Reload the () Class * 1. Use normal functions : Pass in the function name directly * 2. Use class : Pass in an object of this class , Anonymous objects are generally used , That is, the class name () * 3. The function object passed into the priority queue needs to be consistent with the required format . */
/* 3、 ... and . see prior_queue If you have a template , The last two parameter types have default values * If you use the default type . You need to implement the < overloaded */
/* Prepare the function object */
struct cmp {
bool operator ()(ListNode* a, ListNode* b) {
/* return true Under the circumstances , This element moves down in the heap */
return a->val > b->val;
}
};
class Solution {
public:
ListNode* mergeKLists(vector<ListNode*>& lists) {
/* The idea is to put all pointers in a priority queue * The top of the heap has the highest priority . * Every time the top of the stack is ejected * Then judge whether there are subsequent nodes , If there is , stay push Come in , Priority queues restructure the heap */
priority_queue<ListNode*,vector<ListNode*>,cmp> queue;
for (auto node : lists) {
/* It has been stated in the title that an empty linked list may be introduced */
if (node)
queue.push(node);
}
/* Define the storage location of results , First define the header node , And its corresponding pointer ( For unified operation )*/
ListNode ans, * cur=&ans;
/* The final situation is that the queue is empty */
while (!queue.empty()) {
/* Don't think about taking a direct step pop,pop no return value */
ListNode* temp = queue.top();
queue.pop();
/* Direct operation head pointer , To take out temp Receive ans Back */
cur->next = temp;
/* Move the pointer to the end of the linked list , In order to continue splicing */
cur = cur->next;
if (temp->next)
queue.push(temp->next);
}
/* The header node has no practical significance . Don't go back */
return ans.next;
}
};
边栏推荐
- Working principle of genius telephone watch Z3
- MySQL時間、時區、自動填充0的問題
- Kconfig Kbuild
- (五)R语言入门生物信息学——ORF和序列分析
- By v$rman_ backup_ job_ Oracle "bug" caused by details
- RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
- Understanding of AMBA, AHB, APB and Axi
- [Offer18]删除链表的节点
- [esp32 learning-1] construction of Arduino esp32 development environment
- NRF24L01故障排查
猜你喜欢

RT thread API reference manual

2021.11.10汇编考试

MySQL takes up too much memory solution

JS Title: input array, exchange the largest with the first element, exchange the smallest with the last element, and output array.

Redis based distributed ID generator

程序设计大作业:教务管理系统(C语言)

数据库课程设计:高校教务管理系统(含代码)

Amba, ahb, APB, Axi Understanding

Arduino uno R3 register writing method (1) -- pin level state change

基於Redis的分布式ID生成器
随机推荐
Esp8266 connects to onenet cloud platform (mqtt) through Arduino IDE
GCC compilation options
[899]有序队列
Stm32f1+bc20+mqtt+freertos system is connected to Alibaba cloud to transmit temperature and humidity and control LED lights
CUDA C programming authoritative guide Grossman Chapter 4 global memory
【ESP32学习-2】esp32地址映射
Embedded startup process
ARM PC=PC+8 最便于理解的阐述
Pat 1097 duplication on a linked list (25 points)
Flink late data processing (3)
Priority inversion and deadlock
Gateway fails to route according to the service name, and reports an error service unavailable, status=503
Arm pc=pc+8 is the most understandable explanation
[Offer29] 排序的循环链表
Detailed explanation of Union [C language]
Page performance optimization of video scene
Pytorch four commonly used optimizer tests
Pytoch temperature prediction
Basic operations of databases and tables ----- modifying data tables
VSCode基础配置