题目描述
给你一个链表数组,每个链表都已经按升序排列。
请你将所有链表合并到一个升序链表中,返回合并后的链表。
示例 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
个人解法
Java(顺序合并)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
if (lists.length==0){
return null;
}
ListNode l1=lists[0];
for(int i=1;i< lists.length;i++){
l1=mergeTwoLists(l1,lists[i]);
}
return l1;
}
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode prehead = new ListNode(-1);
ListNode prev = prehead;
while (l1 != null && l2 != null) {
if (l1.val <= l2.val) {
prev.next = l1;
l1 = l1.next;
} else {
prev.next = l2;
l2 = l2.next;
}
prev = prev.next;
}
// 合并后 l1 和 l2 最多只有一个还未被合并完,我们直接将链表末尾指向未合并完的链表即可
prev.next = l1 == null ? l2 : l1;
return prehead.next;
}
}
JavaScript
/*
* @lc app=leetcode.cn id=23 lang=javascript
*
* [23] 合并K个升序链表
*/
// @lc code=start
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
var mergeKLists = function (lists) {
let listsArr = [];
let flag = true;
let root = new ListNode(0);
let temp = root;
const len = lists.length
for (let i = 0; i < len; i++) {
if (lists[i]) {
listsArr.push(lists[i]);
}
}
while (flag) {
const nowRootIndex = getMin(listsArr);
if (!listsArr.length) {
flag = false;
break;
}
temp.next = new ListNode(listsArr[nowRootIndex].val);
temp = temp.next;
listsArr[nowRootIndex] = listsArr[nowRootIndex].next;
if (listsArr[nowRootIndex] === null) {
listsArr.splice(nowRootIndex, 1);
}
}
return root.next;
};
function getMin(arr) {
let minIndex = 0;
const len = arr.length;
for (let i = 1; i < len; i++) {
if (arr[i] && arr[i].val < arr[minIndex].val) {
minIndex = i;
}
}
return minIndex;
}
// @lc code=end
其他解法
Java
分治法
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
return merge(lists, 0, lists.length - 1);
}
public ListNode merge(ListNode[] lists, int l, int r) {
if (l == r) {
return lists[l];
}
if (l > r) {
return null;
}
int mid = (l + r) >> 1;
return mergeTwoLists(merge(lists, l, mid), merge(lists, mid + 1, r));
}
public ListNode mergeTwoLists(ListNode a, ListNode b) {
if (a == null || b == null) {
return a != null ? a : b;
}
ListNode head = new ListNode(0);
ListNode tail = head, aPtr = a, bPtr = b;
while (aPtr != null && bPtr != null) {
if (aPtr.val < bPtr.val) {
tail.next = aPtr;
aPtr = aPtr.next;
} else {
tail.next = bPtr;
bPtr = bPtr.next;
}
tail = tail.next;
}
tail.next = (aPtr != null ? aPtr : bPtr);
return head.next;
}
}
优先队列
class Solution {
class Status implements Comparable<Status> {
int val;
ListNode ptr;
Status(int val, ListNode ptr) {
this.val = val;
this.ptr = ptr;
}
public int compareTo(Status status2) {
return this.val - status2.val;
}
}
PriorityQueue<Status> queue = new PriorityQueue<Status>();
public ListNode mergeKLists(ListNode[] lists) {
for (ListNode node: lists) {
if (node != null) {
queue.offer(new Status(node.val, node));
}
}
ListNode head = new ListNode(0);
ListNode tail = head;
while (!queue.isEmpty()) {
Status f = queue.poll();
tail.next = f.ptr;
tail = tail.next;
if (f.ptr.next != null) {
queue.offer(new Status(f.ptr.next.val, f.ptr.next));
}
}
return head.next;
}
}
JavaScript
解法1——顺序合并
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
var mergeKLists = function (lists) {
let temp = null;
const len = lists.length;
for (let i = 0; i < len; i++) {
temp = mergin(temp, lists[i]);
}
return temp;
};
function mergin(list1, list2) {
let node1 = list1;
let node2 = list2;
const root = new ListNode();
let temp = root;
while (node2 && node1) {
if (node1.val <= node2.val) {
temp.next = node1;
node1 = node1.next;
} else {
temp.next = node2;
node2 = node2.next;
}
temp = temp.next;
}
temp.next = node1 ? node1 : node2;
return root.next;
}
解法二——分治合并
考虑优化方法一,用分治的方法进行合并。
将 k 个链表配对并将同一对中的链表合并;
重复这一过程,直到我们得到了最终的有序链表。
/**
* @param {ListNode[]} lists
* @return {ListNode}
*/
var mergeKLists = function (lists) {
if (!lists.length) {
return null;
}
let len = lists.length;
let arr = lists;
while (len > 1) {
let temp = null;
let tempArr = [];
let length = arr.length;
for (let i = 0; i < length; i += 2) {
if (i + 1 < length) {
temp = mergin(arr[i], arr[i + 1]);
tempArr.push(temp);
} else {
tempArr.push(arr[i]);
}
}
arr = [...tempArr];
len /= 2;
}
return arr[0];
};
function mergin(list1, list2) {
let node1 = list1;
let node2 = list2;
const root = new ListNode();
let temp = root;
while (node2 && node1) {
if (node1.val <= node2.val) {
temp.next = node1;
node1 = node1.next;
} else {
temp.next = node2;
node2 = node2.next;
}
temp = temp.next;
}
temp.next = node1 ? node1 : node2;
return root.next;
}
class Solution {
public ListNode mergeKLists(ListNode[] lists) {
return merge(lists, 0, lists.length - 1);
}
public ListNode merge(ListNode[] lists, int l, int r) {
if (l == r) {
return lists[l];
}
if (l > r) {
return null;
}
int mid = (l + r) >> 1;
return mergeTwoLists(merge(lists, l, mid), merge(lists, mid + 1, r));
}
public ListNode mergeTwoLists(ListNode a, ListNode b) {
if (a == null || b == null) {
return a != null ? a : b;
}
ListNode head = new ListNode(0);
ListNode tail = head, aPtr = a, bPtr = b;
while (aPtr != null && bPtr != null) {
if (aPtr.val < bPtr.val) {
tail.next = aPtr;
aPtr = aPtr.next;
} else {
tail.next = bPtr;
bPtr = bPtr.next;
}
tail = tail.next;
}
tail.next = (aPtr != null ? aPtr : bPtr);
return head.next;
}
}