hot 100 第二十七题 27.合并两个有序链表

题目:

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

示例 1:

复制代码
输入:l1 = [1,2,4], l2 = [1,3,4]
输出:[1,1,2,3,4,4]

示例 2:

复制代码
输入:l1 = [], l2 = []
输出:[]

示例 3:

复制代码
输入:l1 = [], l2 = [0]
输出:[0]

核心思路

双指针比较:类似归并排序的合并过程,每次选择两个链表中较小的节点。

复制代码
list1: 1 → 2 → 4
list2: 1 → 3 → 4

合并过程:
比较1和1 → 选1(list1)
比较2和1 → 选1(list2)
比较2和3 → 选2(list1)
比较4和3 → 选3(list2)
比较4和4 → 选4(list1)
剩余4(list2)

结果: 1 → 1 → 2 → 3 → 4 → 4

题解:

java 复制代码
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        // 创建哑节点,简化边界处理
        ListNode dummy = new ListNode(0);
        ListNode curr = dummy;
        
        // 两个链表都还有节点时,选择较小的
        while (list1 != null && list2 != null) {
            if (list1.val <= list2.val) {
                curr.next = list1;
                list1 = list1.next;
            } else {
                curr.next = list2;
                list2 = list2.next;
            }
            curr = curr.next;
        }
        
        // 连接剩余的节点
        if (list1 != null) {
            curr.next = list1;
        }
        if (list2 != null) {
            curr.next = list2;
        }
        
        return dummy.next;
    }
}
```

### 详细演示
```
list1: 1 → 2 → 4
list2: 1 → 3 → 4

创建哑节点:
dummy → null
curr指向dummy

第1次比较:
------------------
list1.val = 1, list2.val = 1
1 <= 1,选list1

dummy → 1
        ↑
      list1

curr移动: curr = curr.next
list1移动: list1 = list1.next

现在:
dummy → 1 → null
        ↑
       curr
list1 = 2
list2 = 1

第2次比较:
------------------
list1.val = 2, list2.val = 1
2 > 1,选list2

dummy → 1 → 1
            ↑
          list2

curr移动,list2移动

现在:
dummy → 1 → 1 → null
            ↑
           curr
list1 = 2
list2 = 3

第3次比较:
------------------
list1.val = 2, list2.val = 3
2 <= 3,选list1

dummy → 1 → 1 → 2
                ↑
              list1

curr移动,list1移动

现在:
dummy → 1 → 1 → 2 → null
                ↑
               curr
list1 = 4
list2 = 3

第4次比较:
------------------
list1.val = 4, list2.val = 3
4 > 3,选list2

dummy → 1 → 1 → 2 → 3
                    ↑
                  list2

curr移动,list2移动

现在:
dummy → 1 → 1 → 2 → 3 → null
                    ↑
                   curr
list1 = 4
list2 = 4

第5次比较:
------------------
list1.val = 4, list2.val = 4
4 <= 4,选list1

dummy → 1 → 1 → 2 → 3 → 4
                        ↑
                      list1

curr移动,list1移动

现在:
dummy → 1 → 1 → 2 → 3 → 4 → null
                        ↑
                       curr
list1 = null
list2 = 4

循环结束(list1为null)
------------------
连接剩余: curr.next = list2

dummy → 1 → 1 → 2 → 3 → 4 → 4
                            ↑
                          list2

返回 dummy.next
结果: 1 → 1 → 2 → 3 → 4 → 4

本质

合并两个有序链表的核心:

  • 归并思想:类似归并排序的合并过程
  • 双指针:分别指向两个链表,选择较小的
  • 哑节点技巧:简化头节点的处理

这是链表操作的基础题,掌握了迭代和递归两种写法,就能应对各种变体。

相关推荐
Navigator_Z4 小时前
LeetCode //C - 1089. Duplicate Zeros
c语言·算法·leetcode
语戚8 小时前
力扣 3161. 块放置查询:线段树解法(Java 实现)
java·算法·leetcode·面试·线段树·力扣·
CS创新实验室8 小时前
从顺序表到动态数组:数据结构的永恒基石与现代语言的优雅封装
数据结构·算法
8Qi810 小时前
LeetCode 23. 合并 K 个升序链表 —— 小顶堆(PriorityQueue)
数据结构·算法·leetcode·链表·
QiLinkOS10 小时前
《打破“用爱发电”:一种基于 Gitee 与时间戳的开源权益分配机制探索》
c语言·数据结构·c++·科技·算法·gitee·开源
Lsk_Smion12 小时前
力扣实训 _ [200].岛屿数量
算法·leetcode·深度优先
Boom_Shu12 小时前
长方形的关系
数据结构·c++·算法
Lsk_Smion13 小时前
力扣实训 _ [543].二叉树的直径 _ [23].合并K个升序列表
数据结构·算法·leetcode
凯瑟琳.奥古斯特14 小时前
力扣1235:加权区间调度最优解
java·python·算法·leetcode·职场和发展
memcpy015 小时前
LeetCode 2144. 打折购买糖果的最小开销【贪心】
算法·leetcode·职场和发展