算法(TS):合并两个有序链表

将两个升序链表合并为一个新的 升序 链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。

示例 1:

输入:l1 = [1,2,4], l2 = [1,3,4] 输出:[1,1,2,3,4,4]

示例 2:

输入:l1 = [], l2 = [] 输出:[]

示例 3:

输入:l1 = [], l2 = [0] 输出:[0]

解答一

  • 先判断是 l1 插入 l2 还是 l2 插入 l1,这里的判断条件是,值大的插入值小的后面,将被插入的链表成为 res,插入链表是 willInsert
  • 遍历 willInsert,当 res.next.val > willInsert.val 时,将 willInsert 插入 res 的下一个位置,否则 res 指针往下移动,查找willInsert下一个可能的插入位置。将 willInsert 插入 res 之后,又继续找 willInsert 的下一个节点的插入位置
ts 复制代码
/**
 * 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 mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
    if (!list1) return list2
    if (!list2) return list1
    // 将 willInsert 上的节点插入 res
    let res: ListNode | null = list1.val < list2.val ? list1 : list2
    let willInsert: ListNode | null = list1.val < list2.val ? list2 : list1
    let result = res

    while(willInsert) {
        while(res) {
            if (!res.next) {
                res.next = willInsert
                willInsert = null
                break
            }
            if(res.next.val > willInsert.val) {
                const temp = res.next
                const temp2:ListNode | null = willInsert.next
                res.next = willInsert
                willInsert.next = temp
                willInsert = temp2
                res = res.next
                break
            } 
            res = res.next
        }
    }

    return result
};

时间复杂度O(n + m),空间复杂O(1)

解答二

创建一个新的链表头,将 l1 和 l2 按照大小顺序插入其中

ts 复制代码
/**
 * 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 mergeTwoLists(list1: ListNode | null, list2: ListNode | null): ListNode | null {
  const newList = new ListNode(-1)
  let current = newList
  if(!list2) return list1
  if (!list1) return list2
  while(list2 && list1) {
    if (list2.val > list1.val) {
      current.next = list1
      list1 = list1.next
    } else {
      current.next = list2
      list2 = list2.next
    }

    current = current.next
  }
  current.next = list2 ? list2 : list1
  return newList.next
};

时间复杂度O(n + m),空间复杂O(1)

相关推荐
星之卡比*几秒前
前端面试题---vite和webpack的区别
前端·面试
^^为欢几何^^5 分钟前
npm、pnpm和yarn有什么区别
前端·npm·node.js
乔冠宇13 分钟前
微信小程序中将图片截图为正方形(自动居中)
微信小程序·小程序·typescript·uniapp
AC-PEACE27 分钟前
Vue 中 MVVM、MVC 和 MVP 模式的区别
前端·vue.js·mvc
播播资源30 分钟前
ChatGPT付费创作系统V3.1.3独立版 WEB端+H5端+小程序端 (DeepSeek高级通道+推理输出格式)安装教程
前端·ai·chatgpt·ai作画·小程序·deepseek·deepseek-v3
夏末秋也凉38 分钟前
力扣-回溯-491 非递减子序列
数据结构·算法·leetcode
penguin_bark40 分钟前
三、动规_子数组系列
算法·leetcode
kyle~1 小时前
thread---基本使用和常见错误
开发语言·c++·算法
zhrb1 小时前
打开Firefox自动打开hao360.hjttif.com标签解决方案
前端·firefox
安大桃子1 小时前
Cesium实现深色地图效果
前端·gis·cesium