当前位置:网站首页>[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;
}
};
边栏推荐
- Arduino JSON data information parsing
- Arduino gets the length of the array
- ES6语法总结--上篇(基础篇)
- Pat 1097 duplication on a linked list (25 points)
- [leetcode622]设计循环队列
- JS正则表达式基础知识学习
- Missing value filling in data analysis (focus on multiple interpolation method, miseforest)
- [Offer29] 排序的循环链表
- 單片機藍牙無線燒錄
- Inline detailed explanation [C language]
猜你喜欢
Basic operations of databases and tables ----- classification of data
(3) Introduction to bioinformatics of R language - function, data Frame, simple DNA reading and analysis
Programmers can make mistakes. Basic pointers and arrays of C language
Redis based distributed locks and ultra detailed improvement ideas
MySQL時間、時區、自動填充0的問題
Kconfig Kbuild
Working principle of genius telephone watch Z3
【ESP32学习-2】esp32地址映射
ES6语法总结--下篇(进阶篇 ES6~ES11)
VSCode基础配置
随机推荐
How to add music playback function to Arduino project
Redis based distributed ID generator
Walk into WPF's drawing Bing Dwen Dwen
ESP8266连接onenet(旧版MQTT方式)
Pat 1097 duplication on a linked list (25 points)
GCC compilation options
Remember an experience of ECS being blown up by passwords - closing a small black house, changing passwords, and changing ports
The dolphin scheduler remotely executes shell scripts through the expect command
Feature of sklearn_ extraction. text. CountVectorizer / TfidVectorizer
[Leetcode15]三数之和
Basic operations of databases and tables ----- creating data tables
Selective sorting and bubble sorting [C language]
JS regular expression basic knowledge learning
@The difference between Autowired and @resource
level16
JUC forkjoin and completable future
VIM command line notes
2022.2.12 resumption
(三)R语言的生物信息学入门——Function, data.frame, 简单DNA读取与分析
Gravure sans fil Bluetooth sur micro - ordinateur à puce unique