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

相关推荐
森焱森32 分钟前
APM与ChibiOS系统
c语言·单片机·算法·架构·无人机
★Orange★42 分钟前
Linux Kernel kfifo 实现和巧妙设计
linux·运维·算法
尘世闲鱼1 小时前
解数独(C++版本)
开发语言·c++·算法·解数独
qqxhb1 小时前
零基础数据结构与算法——第四章:基础算法-排序(中)
数据结构·算法·排序算法·归并·快排·堆排
爱喝水的小周1 小时前
AJAX vs axios vs fetch
前端·javascript·ajax
Jinxiansen02111 小时前
unplugin-vue-components 最佳实践手册
前端·javascript·vue.js
几道之旅1 小时前
介绍electron
前端·javascript·electron
周胡杰1 小时前
鸿蒙arkts使用关系型数据库,使用DB Browser for SQLite连接和查看数据库数据?使用TaskPool进行频繁数据库操作
前端·数据库·华为·harmonyos·鸿蒙·鸿蒙系统
31535669131 小时前
ClipReader:一个剪贴板英语单词阅读器
前端·后端
玲小珑1 小时前
Next.js 教程系列(十一)数据缓存策略与 Next.js 运行时
前端·next.js