BM5 合并k个已排序的链表

This commit is contained in:
轩辕龙儿 2022-07-26 16:45:47 +08:00
parent e6e8711bd3
commit f49dc89756

View File

@ -0,0 +1,38 @@
package com.huangge1199.nowcoder.bm.BM5;
import com.huangge1199.nowcoder.common.ListNode;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.PriorityQueue;
/**
* @author hyy
* @Classname Solution
* @Description BM5 合并k个已排序的链表
* @Date 2022/7/26 16:32
*/
public class Solution {
public ListNode mergeKLists(ArrayList<ListNode> lists) {
PriorityQueue<ListNode> pq = new PriorityQueue<>(Comparator.comparingInt(o -> o.val));
ListNode list = new ListNode(0);
ListNode head = list;
for (ListNode tmp : lists) {
if (tmp != null) {
pq.add(tmp);
}
}
while (!pq.isEmpty()) {
head.next = pq.poll();
head = head.next;
while (head.next == null && !pq.isEmpty()) {
head.next = pq.poll();
head = head.next;
}
if (head.next != null) {
pq.add(head.next);
}
}
return list.next;
}
}