ListNode* mergeKLists(vector<ListNode*>& lists) {
vector<int> vals;
for (ListNode* head : lists) {
for (ListNode* n = head; n; n = n->next) vals.push_back(n->val);
}
sort(vals.begin(), vals.end());
ListNode* dummy = new ListNode(0);
ListNode* tail = dummy;
for (int v : vals) {
tail->next = new ListNode(v);
tail = tail->next;
}
return dummy->next;
}
public ListNode mergeKLists(ListNode[] lists) {
List<Integer> vals = new ArrayList<>();
for (ListNode head : lists) {
for (ListNode n = head; n != null; n = n.next) vals.add(n.val);
}
Collections.sort(vals);
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
for (int v : vals) {
tail.next = new ListNode(v);
tail = tail.next;
}
return dummy.next;
}
def merge_k_lists(lists):
vals = []
for head in lists:
n = head
while n:
vals.append(n.val)
n = n.next
vals.sort()
dummy = ListNode(0)
tail = dummy
for v in vals:
tail.next = ListNode(v)
tail = tail.next
return dummy.next
function mergeKLists(lists: (ListNode | null)[]): ListNode | null {
const vals: number[] = [];
for (const head of lists) {
for (let n = head; n; n = n.next) vals.push(n.val);
}
vals.sort((a, b) => a - b);
const dummy = new ListNode(0);
let tail = dummy;
for (const v of vals) {
tail.next = new ListNode(v);
tail = tail.next;
}
return dummy.next;
}
ListNode* mergeTwo(ListNode* a, ListNode* b) {
ListNode dummy(0);
ListNode* tail = &dummy;
while (a && b) {
if (a->val <= b->val) { tail->next = a; a = a->next; }
else { tail->next = b; b = b->next; }
tail = tail->next;
}
tail->next = a ? a : b;
return dummy.next;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
ListNode* acc = nullptr;
for (ListNode* list : lists) {
acc = mergeTwo(acc, list);
}
return acc;
}
public ListNode mergeKLists(ListNode[] lists) {
ListNode acc = null;
for (ListNode list : lists) {
acc = mergeTwo(acc, list);
}
return acc;
}
private ListNode mergeTwo(ListNode a, ListNode b) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
while (a != null && b != null) {
if (a.val <= b.val) { tail.next = a; a = a.next; }
else { tail.next = b; b = b.next; }
tail = tail.next;
}
tail.next = a != null ? a : b;
return dummy.next;
}
def merge_k_lists(lists):
acc = None
for lst in lists:
acc = merge_two(acc, lst)
return acc
def merge_two(a, b):
dummy = ListNode(0)
tail = dummy
while a and b:
if a.val <= b.val:
tail.next = a
a = a.next
else:
tail.next = b
b = b.next
tail = tail.next
tail.next = a or b
return dummy.next
function mergeKLists(lists: (ListNode | null)[]): ListNode | null {
let acc: ListNode | null = null;
for (const list of lists) {
acc = mergeTwo(acc, list);
}
return acc;
}
function mergeTwo(a: ListNode | null, b: ListNode | null): ListNode | null {
const dummy = new ListNode(0);
let tail = dummy;
while (a && b) {
if (a.val <= b.val) {
tail.next = a;
a = a.next;
} else {
tail.next = b;
b = b.next;
}
tail = tail.next;
}
tail.next = a ?? b;
return dummy.next;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
auto cmp = [](ListNode* x, ListNode* y) { return x->val > y->val; };
priority_queue<ListNode*, vector<ListNode*>, decltype(cmp)> heap(cmp);
for (ListNode* head : lists) if (head) heap.push(head);
ListNode dummy(0);
ListNode* tail = &dummy;
while (!heap.empty()) {
ListNode* node = heap.top(); heap.pop();
tail->next = node;
tail = node;
if (node->next) heap.push(node->next);
}
return dummy.next;
}
public ListNode mergeKLists(ListNode[] lists) {
PriorityQueue<ListNode> heap = new PriorityQueue<>((x, y) -> Integer.compare(x.val, y.val));
for (ListNode head : lists) if (head != null) heap.add(head);
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
while (!heap.isEmpty()) {
ListNode node = heap.poll();
tail.next = node;
tail = node;
if (node.next != null) heap.add(node.next);
}
return dummy.next;
}
def merge_k_lists(lists):
heap = []
for i, head in enumerate(lists):
if head:
heapq.heappush(heap, (head.val, i, head))
dummy = ListNode(0)
tail = dummy
while heap:
_, i, node = heapq.heappop(heap)
tail.next = node
tail = node
if node.next:
heapq.heappush(heap, (node.next.val, i, node.next))
return dummy.next
function mergeKLists(lists: (ListNode | null)[]): ListNode | null {
const heap = new MinHeap<ListNode>((x, y) => x.val < y.val);
for (const head of lists) if (head) heap.push(head);
const dummy = new ListNode(0);
let tail = dummy;
while (heap.size > 0) {
const node = heap.pop();
tail.next = node;
tail = node;
if (node.next) heap.push(node.next);
}
return dummy.next;
}
class MinHeap<T> {
private a: T[] = [];
constructor(private less: (x: T, y: T) => boolean) {}
get size() { return this.a.length; }
push(v: T) {
const a = this.a;
a.push(v);
let i = a.length - 1;
while (i > 0) {
const p = (i - 1) >> 1;
if (!this.less(a[i], a[p])) break;
[a[i], a[p]] = [a[p], a[i]];
i = p;
}
}
pop(): T {
const a = this.a;
const top = a[0];
const last = a.pop()!;
if (a.length) {
a[0] = last;
let i = 0;
for (;;) {
let m = i;
const l = 2 * i + 1, r = l + 1;
if (l < a.length && this.less(a[l], a[m])) m = l;
if (r < a.length && this.less(a[r], a[m])) m = r;
if (m === i) break;
[a[i], a[m]] = [a[m], a[i]];
i = m;
}
}
return top;
}
}
ListNode* mergeTwo(ListNode* a, ListNode* b) {
ListNode dummy(0);
ListNode* tail = &dummy;
while (a && b) {
if (a->val <= b->val) { tail->next = a; a = a->next; }
else { tail->next = b; b = b->next; }
tail = tail->next;
}
tail->next = a ? a : b;
return dummy.next;
}
ListNode* mergeKLists(vector<ListNode*>& lists) {
if (lists.empty()) return nullptr;
for (int gap = 1; gap < (int)lists.size(); gap *= 2) {
for (int i = 0; i + gap < (int)lists.size(); i += 2 * gap) {
lists[i] = mergeTwo(lists[i], lists[i + gap]);
}
}
return lists[0];
}
public ListNode mergeKLists(ListNode[] lists) {
if (lists.length == 0) return null;
for (int gap = 1; gap < lists.length; gap *= 2) {
for (int i = 0; i + gap < lists.length; i += 2 * gap) {
lists[i] = mergeTwo(lists[i], lists[i + gap]);
}
}
return lists[0];
}
private ListNode mergeTwo(ListNode a, ListNode b) {
ListNode dummy = new ListNode(0);
ListNode tail = dummy;
while (a != null && b != null) {
if (a.val <= b.val) { tail.next = a; a = a.next; }
else { tail.next = b; b = b.next; }
tail = tail.next;
}
tail.next = a != null ? a : b;
return dummy.next;
}
def merge_k_lists(lists):
if not lists:
return None
gap = 1
while gap < len(lists):
for i in range(0, len(lists) - gap, 2 * gap):
lists[i] = merge_two(lists[i], lists[i + gap])
gap *= 2
return lists[0]
def merge_two(a, b):
dummy = ListNode(0)
tail = dummy
while a and b:
if a.val <= b.val:
tail.next = a
a = a.next
else:
tail.next = b
b = b.next
tail = tail.next
tail.next = a or b
return dummy.next
function mergeKLists(lists: (ListNode | null)[]): ListNode | null {
if (lists.length === 0) return null;
for (let gap = 1; gap < lists.length; gap *= 2) {
for (let i = 0; i + gap < lists.length; i += 2 * gap) {
lists[i] = mergeTwo(lists[i], lists[i + gap]);
}
}
return lists[0];
}
function mergeTwo(a: ListNode | null, b: ListNode | null): ListNode | null {
const dummy = new ListNode(0);
let tail = dummy;
while (a && b) {
if (a.val <= b.val) {
tail.next = a;
a = a.next;
} else {
tail.next = b;
b = b.next;
}
tail = tail.next;
}
tail.next = a ?? b;
return dummy.next;
}