当前位置:网站首页>Merge K ascending linked lists ---2022/02/26
Merge K ascending linked lists ---2022/02/26
2022-06-11 17:34:00 【City Pig】
List of articles
Title Description
Here's an array of linked lists , Each list has been listed in ascending order .
Please merge all the linked lists into one ascending list , Return the merged linked list .
Title source
Their thinking
Here are two solutions to consider :
The first is queue first . The train of thought is , Put all the head nodes of the linked list into the priority queue .( Additional explanation : Priority queues are different from FIFO queues , Each time, the element with the highest priority is taken out . By default , Priority queue elements are sorted in natural order , That is, the one with a small number is at the head of the team , Strings are sorted in dictionary order . If you need to change the priority setting , Need to achieve Comparator Interface .) Then minimize the node value ( It involves priority reset ) Pop up node , Add to the new linked list , Then add the next node of the node to the queue , Until the queue is empty .
The second is divide and conquer . That is, one is divided into two ( Do pre and post processing ) Thought , Merge two ascending linked lists when you need to use the function of . Until it can't be divided , Then a recursive .
This topic adopts the method of queue first .
Code implementation
The code implementation is as follows :
public ListNode mergeKLists(ListNode[] lists) {
if(lists.length==0) return null;
if(lists.length==1) return lists[0];
ListNode head=new ListNode(0);
ListNode cur=head;
PriorityQueue<ListNode> pq=new PriorityQueue<>(new Comparator<ListNode>() {
public int compare(ListNode o1,ListNode o2) {
return o1.val-o2.val;// Do ascending processing , The small one is at the head of the team
// return o2.val-o1.val;// Do ascending processing , The small one is at the head of the team
}
});
for(ListNode list:lists) {
if(list==null)continue;
pq.add(list);
}
while(!pq.isEmpty()) {
ListNode nextNode=pq.poll();
cur.next=nextNode;
cur=cur.next;
if(nextNode.next!=null) {
pq.add(nextNode.next);
}
}
return head.next;
}
Performance evaluation

Found a confusing operation . Repeatedly submit code , Memory performance continues to degrade , Time performance is not affected .
边栏推荐
- 7-2 h0107. Pig-Latin
- ffmpeg硬编解码 Inter QSV
- tidb-gc相关问题
- 搜狐全员遭诈骗,暴露哪些问题?
- CLP information -5 keywords to see the development trend of the financial industry in 2022
- 为什么udp流设置1316字节
- Vscode automatic eslint formatting when saving code
- Use of forcescan in SQL server and precautions
- Export data prompt -- solution to the problem of secure file priv option
- which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_ mod
猜你喜欢

Axi protocol Basics

MFSR:一种新的推荐系统多级模糊相似度量

活动 | Authing 首次渠道合作活动圆满落幕

Read and understand the development plan for software and information technology service industry during the "14th five year plan"

如何成为一个乐观派组织?

搜狐全员遭诈骗,暴露哪些问题?

测试基础之:黑盒测试

How does Sister Feng change to ice?

Typescript learning notes (II)

Threejs uses indexeddb cache to load GLB model
随机推荐
which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_ mod
adb 命令学习笔记
如何成为一个乐观派组织?
端口规划与APJ
04_特征工程—特征选择
tidb-ddl的速度的调整
Set object mapping + scene 6-face mapping + create space in threejs
Vscode configures eslint to automatically format an error "auto fix is enabled by default. use the single string form“
Cs0006 C failed to find metadata file "c:\users\... Problem
Service learning notes 04 other service implementation methods and alternative methods
信息安全数学基础 Chapter 3——有限域(二)
6-7 文件读写操作
【题解】Codeforces Round #798 (Div. 2)
Semaphore PV operation of process interaction and its code implementation
Recyclerview cache reuse analysis, source code interpretation
Test basis: black box test
What problems are exposed when all Sohu employees are cheated?
QLineEdit 设置输入掩码
Mathematical foundations of information security Chapter 3 - finite fields (II)
信息安全数学基础 Chapter 1——整除