概述
给你一个链表数组,每个链表都已经按升序排列。
请你将所有链表合并到一个升序链表中,返回合并后的链表。
示例 1:
输入:lists = [[1,4,5],[1,3,4],[2,6]]
输出:[1,1,2,3,4,4,5,6]
解释:链表数组如下:
[
1->4->5,
1->3->4,
2->6
]
将它们合并到一个有序链表中得到。
1->1->2->3->4->4->5->6
示例 2:
输入:lists = []
输出:[]
示例 3:
输入:lists = [[]]
输出:[]
提示:
k == lists.length
0 <= k <= 10^4
0 <= lists[i].length <= 500
-10^4 <= lists[i][j] <= 10^4
lists[i] 按 升序 排列
lists[i].length 的总和不超过 10^4
思路一:数组策略
代码如下:
public class Solution9 {
public ListNode mergeKLists(ListNode[] lists) {
if (lists==null||lists.length==0) return null;
ArrayList<ListNode> listNodes = new ArrayList<>();
for (ListNode listNode : lists) {
while (listNode!=null){
listNodes.add(listNode);
listNode=listNode.next;
}
}
listNodes.sort((ListNode l1,ListNode l2)->{
return l1.val-l2.val;
});
ListNode head = new ListNode(0);
ListNode cur = head;
for (ListNode listNode : listNodes) {
cur = cur.next = listNode;
}
return head.next;
}
}
思路二:迭代
代码如下:
public ListNode mergeKLists(ListNode[] lists) {
if (lists==null||lists.length==0) return null;
ListNode head = new ListNode(0);
ListNode cur = head;
while (true) {
int minIndex = -1;
for (int i = 0; i < lists.length; i++) {
if (lists[i] == null) continue;
if (minIndex == -1 || lists[i].val < lists[minIndex].val) {
minIndex = i;
}
}
if (minIndex==-1) break;
cur = cur.next = lists[minIndex];
lists[minIndex] = lists[minIndex].next;
}
return head.next;
}
思路三:两两比较合并
代码如下:
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists==null||lists.length==0) return null;
for (int i = 1; i < lists.length ; i++) {
lists[0] = mergeTwo(lists[0],lists[i]);
}
return lists[0];
}
public ListNode mergeTwo(ListNode l1,ListNode l2){
if (l1==null) return l2;
if (l2==null) return l1;
ListNode head = new ListNode(0);
ListNode cur = head;
while (l1!=null&&l2!=null){
if (l1.val<l2.val){
cur = cur.next = l1;
l1 = l1.next;
}else {
cur = cur.next = l2;
l2 = l2.next;
}
}
if (l1==null) cur = cur.next = l2;
if (l2==null) cur = cur.next = l1;
return head.next;
}
}
思路四:最小堆
代码如下:
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists==null||lists.length==0) return null;
PriorityQueue<ListNode> queue = new PriorityQueue<>((ListNode l1,ListNode l2)->{
return l1.val - l2.val;
});
ListNode head = new ListNode(0);
ListNode cur = head;
for (int i = 0; i < lists.length; i++) {
if (lists[i]==null) continue;
queue.offer(lists[i]);
}
while (!queue.isEmpty()){
ListNode poll = queue.poll();
cur = cur.next = poll;
if (poll.next!=null){
queue.offer(poll.next);
}
}
return head.next;
}
}
思路五:分治算法
代码如下:
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists==null||lists.length==0) return null;
int times = 1;
while (times<lists.length) {
for (int i = 0; i +times< lists.length; i+=times<<1) {
lists[i] = mergeTwo(lists[i], lists[i + times]);
}
times = times<<1;
}
return lists[0];
}
public ListNode mergeTwo(ListNode l1,ListNode l2){
if (l1==null) return l2;
if (l2==null) return l1;
ListNode head = new ListNode(0);
ListNode cur = head;
while (l1!=null&&l2!=null){
if (l1.val<l2.val){
cur = cur.next = l1;
l1 = l1.next;
}else {
cur = cur.next = l2;
l2 = l2.next;
}
}
if (l1==null) cur = cur.next = l2;
if (l2==null) cur = cur.next = l1;
return head.next;
}
}
最后
以上就是想人陪电灯胆为你收集整理的合并K个升序列表(题目来自LeetCode)的全部内容,希望文章能够帮你解决合并K个升序列表(题目来自LeetCode)所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复