7.LeetCode之合并两个有序链表

本文详细介绍了如何合并两个已排序的链表,包括基础解法和递归解法。基础解法中,通过两个指针比较节点值,并不断更新链表,处理边界条件,如空链表和链表合并完成后的情况。递归解法则通过比较节点值决定在哪个链表中继续合并,直至其中一个链表为空。整个过程强调了链表操作和边界条件的处理技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.题目描述

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

示例

在这里插入图片描述

2.基础解法

2.1 解法思路

分别让两个指针指向l1和l2链表,开始比较指针指向的数值
如果l1小,则将l1指向的值并入最终链表,l1指向下一位

2.2 一些小问题

2.2.1 链表的创建与返回

(1) 因为链表需要不停的连接到尾部,因此需要两个指针分别是head和tail
先让他们都指向空值,然后初始化,最后往tail后链接节点,返回head。例如:
// 先让他们都指向空值
ListNode tail = null, head = null;
'''
'''
// 然后初始化
if(head == null)
	head = tail = new ListNode(value1);
// 最后往tail后链接节点
tail.next = new ListNode(value1);
	tail = tail.next;
// 返回head
return head;

但是上面这种做法再合并链表时比较麻烦,需要判断两次head:

if (value1 <= value2) {
    if(head == null)
        head = tail = new ListNode(value1);
    else{
        tail.next = new ListNode(value1);
        tail = tail.next;
    }
    l1 = l1.next;
}
else {
    if(head == null)
        head = tail = new ListNode(value2);
    else {
        tail.next = new ListNode(value2);
        tail = tail.next;
    }
    l2 = l2.next;
}

因此,可以使用比较简单的方法,更改初始化方法,最后返回head.next即可

(2) 优化后的创建列表方法
// 直接初始化
ListNode head = new ListNode(0);
ListNode tail = head;
// 往tail后链接节点
tail.next = new ListNode(value1);
tail = tail.next;
// 返回head.next
return head.next;

因此可以得到比较简单的表达:

while (l1 != null && l2 != null) {
    int value1 = l1.val;
    int value2 = l2.val;
    if (value1 <= value2) {
        tail.next = l1;
        tail = tail.next;
        l1 = l1.next;
    }
    else {
        tail.next = l2;
        tail = tail.next;
        l2 = l2.next;
    }
}

2.2.2 边界条件的处理

(1)当初始一个链表为空时
当初始一个链表为空时,需要进行判断,如果l1为空,直接返回l2
if (l1 == null)
    return l2;
if (l2 == null)
    return l1;
(2) 当合并链表时有一个链表合并完后
当合并链表时有一个链表合并完后,例如当l1合并完,则直接将l2加入tail
if (l1 == null)
    tail.next = l2;
else
    tail.next = l1;

2.3代码

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        ListNode head = new ListNode(0);
        ListNode tail = head;
        if (l1 == null)
            return l2;
        if (l2 == null)
            return l1;
        while (l1 != null && l2 != null) {
            int value1 = l1.val;
            int value2 = l2.val;
            if (value1 <= value2) {
                tail.next = new ListNode(value1);
                // 或者tail.next = l1;
                tail = tail.next;
                l1 = l1.next;
            }
            else {
                tail.next = new ListNode(value2);
                tail = tail.next;
                l2 = l2.next;
            }
        }
        if (l1 == null)
            tail.next = l2;
        else
            tail.next = l1;
    return head.next;
    }
}

3. 递归解决

3.1 思路

递归地定义两个链表里的 merge 操作(忽略边界情况,比如空链表等):

在这里插入图片描述

3.2 算法

class Solution {
    public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
        if (l1 == null) {
            return l2;
        } else if (l2 == null) {
            return l1;
        } else if (l1.val < l2.val) {
            l1.next = mergeTwoLists(l1.next, l2);
            return l1;
        } else {
            l2.next = mergeTwoLists(l1, l2.next);
            return l2;
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值