当前位置:网站首页>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 .
边栏推荐
猜你喜欢

which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_ mod

adb 命令学习笔记

删除链表的倒数第N个节点---2022/02/22

DFS and BFS notes (I) breadth first search based on C language

Derivation of child numbering formula for nodes numbered I in full k-ary tree

Authing biweekly news: authing forum launched (4.25-5.8)

搜狐全員遭詐騙,暴露哪些問題?

QLineEdit 设置输入掩码

论文阅读 dyngraph2vec: Capturing Network Dynamics using Dynamic Graph Representation Learning

Authing CEO 谢扬入选福布斯 2021 年 30 Under 30 亚洲榜单
随机推荐
为什么udp流设置1316字节
threejs中设置物体的贴图+场景的6面贴图 +创建空间
05_特征工程—降维
R language to find missing value location of data set
6-3 读文章(*)
如何成为一个乐观派组织?
Mathematical basis of information security Chapter 1 - Division
Export data prompt -- solution to the problem of secure file priv option
Tidb CDC create task error unknown or incorrect time zone
Authing 双周动态:应用市场上线(5 .10 —5 .22 )
Vscode configures eslint to automatically format with an error "the setting is deprecated. use editor.codeactionsonsave instead with a source“
Service学习笔记02-实战 startService 与bindService
聚类方法汇总
6-6 批量求和(*)
Derivation of child numbering formula for nodes numbered I in full k-ary tree
C language: use H and C. summary of problems encountered in documents
How does Sister Feng change to ice?
【深度学习基础】神经网络的学习(3)
活动 | Authing 首次渠道合作活动圆满落幕
通过Xshell连接有跳板机/堡垒机的服务器