21. 合并两个有序链表

一、题目描述

在这里插入图片描述

二、示例

在这里插入图片描述

三、难度

简单

四、代码

Java版

4.1 法一:迭代法

时间复杂度O(n+m),依次比较两升序链表结点值

/**
 * @author Kidd
 * @create 2022-04-23 21:00
 */
class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }
public class Solution {

    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        //final修饰引用类型变量指的是它里面的地址不能变(不能再指向其他变量),但这个地址所指向的对象或数组的内容不可以变(除非用static final)
        // 头节点
        ListNode mergeList = new ListNode(-1);

        ListNode tempList1 = list1, tempList2 = list2, p = mergeList;
        //遍历两个升序链表,只要其中一个链表遍历完,则结束
        while (tempList1 != null && tempList2 != null) {

            if(tempList1.val < tempList2.val) {
                p.next = tempList1;
                tempList1 = tempList1.next;
            }
            else {
                p.next = tempList2;
                tempList2 = tempList2.next;
            }
            p = p.next;
        }
        //若有没遍历完的链表,则之间连接tempList1
        if(tempList2 != null) {
            p.next = tempList2;
        }
        if (tempList1 != null) {
            p.next = tempList1;
        }

        return mergeList.next;
    }

    public static void main(String[] args) {
        //l1 -> node1 -> node2
        ListNode node2 = new ListNode(5);
        ListNode node1 = new ListNode(2, node2);
        ListNode l1 = new ListNode(1, node1);

        //l2 -> n1 -> node2
        ListNode n2 = new ListNode(9);
        ListNode n1 = new ListNode(7, n2);
        ListNode l2 = new ListNode(4, n1);

        ListNode merge = new Solution().mergeTwoLists(l1, l2);

        while (merge != null) {
            System.out.print(merge.val+" ");
            merge = merge.next;
        }

    }
}

4.2 法二:递归法

class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }
public class Solution {

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

    public static void main(String[] args) {
        //l1 -> node1 -> node2
        ListNode node2 = new ListNode(5);
        ListNode node1 = new ListNode(2, node2);
        ListNode l1 = new ListNode(1, node1);

        //l2 -> n1 -> node2
        ListNode n2 = new ListNode(9);
        ListNode n1 = new ListNode(7, n2);
        ListNode l2 = new ListNode(4, n1);

        ListNode merge = new Solution().mergeTwoLists(l1, l2);

        while (merge != null) {
            System.out.print(merge.val+" ");
            merge = merge.next;
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值