xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

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

image

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;
}



https://leetcode.com/problems/merge-k-sorted-lists/solutions/5302662/leetcode-23-merge-k-sorted-lists-solution-all-in-one/

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, 禁止转载 🈲️,侵权必究⚠️!


posted @   xgqfrms  阅读(6)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 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 说明书
点击右上角即可分享
微信分享提示