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/

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

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-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2024-06-13 00:00  xgqfrms  阅读(4)  评论(0编辑  收藏  举报