LeetCode 23. Merge k Sorted Lists solutions All In One
LeetCode 23. Merge k Sorted Lists solutions All In One
TypeScript
solutions
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function mergeKLists(lists: Array<ListNode | null>): ListNode | null {
let arr = [];
for(let list of lists) {
let head = list;
while(head) {
arr.push(head.val);
head = head.next;
}
}
arr.sort((a, b) => a - b);
// linked-list generator
return listGenerator(arr) ?? null;
};
function listGenerator(arr: Array<number>): ListNode | null {
if(!arr.length) {
return null;
}
let root = new ListNode(arr[0]);
let head = root;
for(let i = 1; i < arr.length; i++) {
let node = new ListNode(arr[i], null);
head.next = node;
head = head.next;
}
return root;
}
Runtime 76ms Beats 95.65% of users with TypeScript
https://leetcode.com/problems/merge-k-sorted-lists/
demos
/**
* Definition for singly-linked list.
* class ListNode {
* val: number
* next: ListNode | null
* constructor(val?: number, next?: ListNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
* }
*/
function mergeKLists(lists: Array<ListNode | null>): ListNode | null {
// linked-list generator
// console.log(`lists `, lists)
// console.log(`lists[0] `, lists[0])
let arr = [];
for(let list of lists) {
// arr.push(...list);
let head = list;
while(head) {
arr.push(head.val);
head = head.next;
}
}
arr.sort((a, b) => a - b);
// console.log(`arr =`, arr);
return listGenerator(arr) ?? null;
};
function listGenerator(arr: Array<number>): ListNode | null {
if(!arr.length) {
return null;
}
let root = new ListNode(arr[0]);
let head = root;
for(let i = 1; i < arr.length; i++) {
let node = new ListNode(arr[i], null);
head.next = node;
head = head.next;
}
console.log(`root =`, root);
return root;
}
ListNode
链表节点
class ListNode {
constructor(val, next) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
// add
// remove
}
// Definition for singly-linked list.
class ListNode {
val: number
next: ListNode | null
constructor(val?: number, next?: ListNode | null) {
this.val = (val===undefined ? 0 : val)
this.next = (next===undefined ? null : next)
}
// add
// remove
}
Linked List Generator
链表生成器
function listGenerator(arr: Array<number>): ListNode | null {
if(!arr.length) {
return null;
}
let root = new ListNode(arr[0]);
let head = root;
for(let i = 1; i < arr.length; i++) {
let node = new ListNode(arr[i], null);
head.next = node;
head = head.next;
}
// console.log(`root =`, root);
return root;
}
refs
js Linked List Generator All In One
https://www.cnblogs.com/xgqfrms/p/16637915.html
https://zzk.cnblogs.com/my/s/blogpost-p?Keywords=list generator
©xgqfrms 2012-2025
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/18244980
未经授权禁止转载,违者必究!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)
2022-06-13 2022 最新字节跳动前端面试算法题 All In One
2022-06-13 how to use js set HTML5 Video default volume value All In One
2022-06-13 HTML5 video controlslist All In One
2022-06-13 一道有疑问的小学二年级数学题 All In One
2021-06-13 macOS 格式化 U盘 到 NTFS 格式 All In One
2021-06-13 React 与 Webpack 项目中集成 TypeScript All In One
2021-06-13 Xbox Series X 说明书