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

相关推荐
岁忧43 分钟前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
chao_7891 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
一斤代码1 小时前
vue3 下载图片(标签内容可转图)
前端·javascript·vue
中微子1 小时前
React Router 源码深度剖析解决面试中的深层次问题
前端·react.js
光影少年1 小时前
从前端转go开发的学习路线
前端·学习·golang
中微子2 小时前
React Router 面试指南:从基础到实战
前端·react.js·前端框架
3Katrina2 小时前
深入理解 useLayoutEffect:解决 UI "闪烁"问题的利器
前端·javascript·面试
秋说3 小时前
【PTA数据结构 | C语言版】一元多项式求导
c语言·数据结构·算法
前端_学习之路3 小时前
React--Fiber 架构
前端·react.js·架构
Maybyy3 小时前
力扣61.旋转链表
算法·leetcode·链表