leetcode一步解决链表合并问题

1、合并两个有序链表

21.合并两个升序链表

题目描述:

 思路:合并两个有序链表也是一个很经典的题目,看到这道题目,不少的小伙伴估计会有种似曾相识的感觉,是不是有点像,合并两个数组哈哈哈哈,所以我们首先先用笨方法(依次比较)l来试试

1、依次比较

看看代码:

public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode cur = new ListNode(0);
        ListNode result = cur;
        ListNode temp1 = list1;
        ListNode temp2 = list2;
        while (temp1 != null && temp2 != null){
            if (temp1.val < temp2.val){
                cur.next = temp1;
                temp1 = temp1.next;
                cur = cur.next;
            }else {
                cur.next = temp2;
                temp2 = temp2.next;
                cur = cur.next;
            }
        }
        if (temp1 == null){
            while (temp2 != null){
                cur.next = temp2;
                temp2 = temp2.next;
                cur = cur.next;
            }
        }else if (temp2 == null){
            while (temp1 != null){
                cur.next = temp1;
                temp1 = temp1.next;
                cur = cur.next;
            }
        }
        return result.next;
    }

时间复杂度:O(n)

空间复杂度:O(1)

2、递归

思路:既然是递归我们就会想到这道题的递归三要素

  • 终止条件:list1为空,或者list2为空
  • 返回值:每一层都返回已经排序好的链表
  • 本机递归内容:如果list1.val更小,则将list1.next和下一级排序好的链表连接;list2反之亦然

我们看看代码:

public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null) return list2;
        if (list2 == null) return list1;
        if (list1.val<list2.val){
            list1.next = mergeTwoLists(list1.next,list2);
            return list1;
        }else {
            list2.next = mergeTwoLists(list1,list2.next);
            return list2;
        }
    }

时间复杂度:O(m+n),m为list1的长度,n为list2的长度

2、合并k个升序链表

23.合并k个升序链表

题目描述:

思路:拿到题目,首先我们先审题,合并k条,其实本质上和合并2条升序链表没有区别,我们只需要拆分,把k条链表,拆分成若干个2条链表,思路瞬间是不是清晰了!

至于如何拆分,我不知道大家有没有了解过 归并排序 ,大家又不了解的可以移步归并排序

归并排序里面用到的思想就是分治算,先分后治,先拆分,后合并,是不是跟咱们这道题有异曲同工之妙,先把链表拆分,再把链表合并;唯一不同的是归并排序时,我们是拆分同一个数组,而现在我们是拆分k条不同的链表,不过我们要得到的效果都是一样的!

了解完之后,我们不妨看看代码:

 public ListNode mergeKLists(ListNode[] lists) {
        if (lists.length < 1) return null;
        return mergeLists(lists, 0, lists.length);
    }
  
    public ListNode mergeLists(ListNode[] lists, int left, int right) {
        if (left >= right) return lists[left];
        int mid = (left + right) / 2;
        //先分
        ListNode l1 = mergeLists(lists, left, mid);
        ListNode l2 = mergeLists(lists, mid + 1, right);
        //后治
        return mergeTwoLists(l1, l2);

    }

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if (list1 == null) return list2;
        if (list2 == null) return list1;

        if (list1.val < list2.val) {
            list1.next = mergeTwoLists(list1.next, list2);
            return list1;
        } else {
            list2.next = mergeTwoLists(list1, list2.next);
            return list2;
        }
    }

这里为了优化我们合并算法的时间复杂度,我们可以采用迭代算法,也就是第一题的第一张方法替代这个合并递归方法,会加快不少时间

public ListNode mergeKLists(ListNode[] lists) {
        if (lists.length < 1) return null;
        return mergeLists(lists, 0, lists.length);
    }

    public ListNode mergeLists(ListNode[] lists, int left, int right) {
        if (left >= right) return lists[left];
        int mid = (left + right) / 2;
        //先分
        ListNode l1 = mergeLists(lists, left, mid);
        ListNode l2 = mergeLists(lists, mid + 1, right);
        //后治
        return mergeTwoLists(l1, l2);

    }
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        ListNode cur = new ListNode(0);
        ListNode result = cur;
        ListNode temp1 = list1;
        ListNode temp2 = list2;
        while (temp1 != null && temp2 != null){
            if (temp1.val < temp2.val){
                cur.next = temp1;
                temp1 = temp1.next;
                cur = cur.next;
            }else {
                cur.next = temp2;
                temp2 = temp2.next;
                cur = cur.next;
            }
        }
        if (temp1 == null){
            while (temp2 != null){
                cur.next = temp2;
                temp2 = temp2.next;
                cur = cur.next;
            }
        }else if (temp2 == null){
            while (temp1 != null){
                cur.next = temp1;
                temp1 = temp1.next;
                cur = cur.next;
            }
        }
        return result.next;
    }

总结:合并升序链表,也是笔试常考的一道题,大家很有必要学习一下~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
本题要求合并两个有序链表。对于链表中的每一个节点,由于链表是有序的,所以可以将两个链表中的节点按照大小顺序进行比较,然后逐个将较小的节点链接上去,最终得到一个新的有序链表。 使用Rust语言实现这个问题,需要首先定义一个链表节点的结构体,包含node值以及next指针。然后定义一个函数来合并两个有序链表,输入为两个链表的头节点指针,输出为新的有序链表的头节点指针。 在合并过程中,首先需要判断两个链表的头节点哪一个较小,将较小的节点作为新链表的头节点,并将该节点的next指针指向递归调用合并函数的结果。递归结束条件为其中一个链表为空,则将另一个链表直接链接到新链表上。 完整代码如下: ```rust // 定义链表节点结构体 #[derive(Debug)] struct ListNode { val: i32, next: Option<Box<ListNode>>, } impl ListNode { fn new(val: i32) -> Self { ListNode { val, next: None } } } // 合并两个有序链表 fn merge_two_lists(l1: Option<Box<ListNode>>, l2: Option<Box<ListNode>>) -> Option<Box<ListNode>> { match (l1, l2) { (None, None) => None, // 两个链表均为空 (l1, None) => l1, // 其中一个链表为空,直接返回另一个链表 (None, l2) => l2, (Some(mut l1), Some(mut l2)) => { if l1.val < l2.val { l1.next = merge_two_lists(l1.next, Some(l2)); Some(l1) } else { l2.next = merge_two_lists(Some(l1), l2.next); Some(l2) } } } } // 测试代码 fn main() { let l1 = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 2, next: Some(Box::new(ListNode { val: 4, next: None, })), })), })); let l2 = Some(Box::new(ListNode { val: 1, next: Some(Box::new(ListNode { val: 3, next: Some(Box::new(ListNode { val: 4, next: None, })), })), })); let merged = merge_two_lists(l1, l2); println!("{:?}", merged); } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

你食不食油饼

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

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

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

打赏作者

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

抵扣说明:

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

余额充值