给你一个链表数组,每个链表都已经按升序排列。
请你将所有链表合并到一个升序链表中,返回合并后的链表。
示例 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 = [[]]
输出:[]
/*** 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 n = lists.length;// if (n == 0) return null;// let mergeTwoLists = (l1, l2) => {// if (l1 == null) return l2;// if (l2 == null) return l1;// if (l1.val <= l2.val) {// l1.next = mergeTwoLists(l1.next, l2);// return l1;// } else {// l2.next = mergeTwoLists(l1, l2.next);// return l2;// }// }// let merge = (left, right) => {// if (left == right) return lists[left];// let mid = (left + right) >> 1;// let l1 = merge(left, mid);// let l2 = merge(mid + 1, right);// return mergeTwoLists(l1, l2);// }// return merge(0, n - 1);// js 骚操作return lists.reduce((p, n) => {while (n) { p.push(n), n = n.next } { return p }}, []).sort((a, b) => a.val - b.val).reduceRight((p, n) => (n.next = p, p = n, p), null)};

