《力扣每日一题》—— 合并两个有序链表

✅作者简介:大家好,我是小鱼儿,大家可以叫我鱼儿

📒博客首页:是小鱼儿哈

🔥系列专栏:一起刷好题

🌻每日一句:努力不是重点,常态化才是关键。真正努力的人,能随时进入任何角色,在过程中找到感觉和快乐。

💖博主也在学习阶段,如发现问题请告知,非常感谢💖

原题链接合并两个有序链表——力扣 

迭代法

一开始,没什么好的思路,只能老老实实的迭代

思路:

当 l1 和 l2 都不是空链表时,判断 l1 和 l2 哪一个链表的头节点的值更小,将较小值的节点添加到结果里,当一个节点被添加到结果里之后,将对应链表中的节点向后移一位。

🍑一开始的代码(里面有很多无用的代码)放在这就是让自己长长记性😂

class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode dummyHead = new ListNode(0); // 定义一个虚拟头结点
        if (list1 == null) {
            return list2; // 处理链表为空的情况
        }
        if (list2 == null) {
            return list1;
        }
        if (list1.val < list2.val) {
            dummyHead.next = list1;
            list1 = list1.next;
        }
        else {
            dummyHead.next = list2;
            list2 = list2.next;
        }
        // 以上部分都是为了确定好新链表的真实头结点,
        // 但后来我才发现,完全没必要,直接用虚拟头结点操作就好,我写了很多无用的代码
        // 不能直接使用头结点,否则容易造成链表的丢失
        ListNode cur = dummyHead.next;
        while (list1 != null && list2 != null) { // 在两个链表不为空的情况下,按大小构建一个新的链表
            if (list1.val < list2.val) {
                cur.next = list1;  // 因为list1此时比list2小,所有cur指向list1
                cur = cur.next;     // 更新cur和list1, 注意list2不用更新
                list1 = list1.next;
            }
            else {
                cur.next = list2;
                cur = cur.next;
                list2 = list2.next;
            }
        }
        while (list1 != null) { // 当list2被合并完后,直接把剩下的list1添加到新的链表中
            cur.next = list1;
            cur = cur.next;
            list1 = list1.next;
        }
        while (list2 != null) { // 当list1被合并完后,直接把剩下的list2添加到新的链表中
            cur.next = list2;
            cur = cur.next;
            list2 = list2.next;
        }
        return dummyHead.next; // 返回新链表的头结点
    }
}

🍑优化后的代码

class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode dummyHead = new ListNode(0); // 定义一个虚拟头结点
        ListNode cur = dummyHead;
        while (list1 != null && list2 != null) { // 在两个链表不为空的情况下,按大小构建一个新的链表
            if (list1.val < list2.val) {
                cur.next = list1;  // 因为list1此时比list2小,所以cur指向list1
                cur = cur.next;     // 更新cur和list1, 注意list2不用更新
                list1 = list1.next;
            }
            else {
                cur.next = list2;
                cur = cur.next;
                list2 = list2.next;
            }
        }
        if (list1 != null) { // 当list2被合并完后,直接把剩下的list1添加到新的链表中
            cur.next = list1;
        }
        if (list2 != null) { // 当list1被合并完后,直接把剩下的list2添加到新的链表中
            cur.next = list2;
        }
        return dummyHead.next; // 返回新链表的头结点
    }
}

执行结果: 

 


递归写法

对于递归我们考虑的无法就两个:

  1. 递归是怎样结束的?
  2. 函数是如何进行递归操作的?

📝对于本题:

  • 当两个链表中的任意一个为空时,递归就结束了。 
  • 如何递归:我们判断 l1 和 l2 头结点哪个更小,然后较小结点的 next 指针指向其余结点的合并结果。(调用递归)

如图所示: 

代码如下: 

class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null) return list2; // 当list1此时为空,直接把剩下的List2接上
        if (list2 == null) return list1; // 当list2此时为空,直接把剩下的List1接上
        if (list1.val <= list2.val) {
            list1.next = mergeTwoLists(list1.next, list2); // 递归中的递
            return list1;  // 递归中的归
        } 
        else {
            list2.next = mergeTwoLists(list1, list2.next);
            return list2;
        }
    }
}

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是小鱼儿哈

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值