算法(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)

相关推荐
其实防守也摸鱼1 分钟前
Linux文本处理命令理论学习
linux·服务器·前端·数据库·sql·学习·安全
Pniubi4 分钟前
快速排序(力扣基础)(递归)
java·数据结构·算法
roman_日积跬步-终至千里20 分钟前
【数据Agent论文·分析与报告评测|27】ComInsight:原子洞察正确,组合后的结论就可靠吗?
人工智能·算法·机器学习·数据分析
涛涛ing30 分钟前
Chrome DevTools MCP 正式可用:AI 终于能自己开浏览器调试你的页面了
前端
Samson Bruce38 分钟前
【数学之美】
线性代数·算法·概率论
after you41 分钟前
【无标题】
前端·javascript
酷炫码神43 分钟前
Day 6|ES6+ 语法:把现代 JS 写顺手,从解构到 map / filter / reduce
前端·javascript·es6
Shockang1 小时前
TanStack Router 实战:URL 校验、Loader 与路由权限
typescript·react·路由·前端开发·tanstack router
lemon_sjdk1 小时前
从一段真实的 Electron IM 前端代码,看高级前端的工程化修养
前端·electron·状态模式·im
Mister Leon1 小时前
Nvidia Orin AGX实战之从量化到蒸馏:在 Orin 上落地的大模型压缩实战
人工智能·算法·机器学习