剑指Offer:合并两个排序的链表

描述

输入两个递增的链表,单个链表的长度为n,合并这两个链表并使新链表中的节点仍然是递增排序的。
数据范围: 0 \le n \le 10000≤n≤1000,-1000 \le 节点值 \le 1000−1000≤节点值≤1000
要求:空间复杂度 O(1)O(1),时间复杂度 O(n)O(n)

如输入{1,3,5},{2,4,6}时,合并后的链表为{1,2,3,4,5,6},所以对应的输出为{1,2,3,4,5,6},转换过程如下图所示:
在这里插入图片描述
或输入{-1,2,4},{1,3,4}时,合并后的链表为{-1,1,2,3,4,4},所以对应的输出为{-1,1,2,3,4,4},转换过程如下图所示:
在这里插入图片描述

我的思路

要合并两个排序的链表需要注意的就是两个节点互相之间的大小,只需要判断每下一个的节点大小,小的放进去就好。这是我第一遍代码的思路

public ListNode Merge(ListNode list1,ListNode list2) {
        //这里我开始思考要不要新建一个节点
        ListNode res;
        if (list1 == null && list2 == null)
            return null;
//        res = list1.val < list2.val ? list1 : list2;//先放一个值进去,这样后面循环里就直接res.next就好
        //这个不需要
        while (list1 != null && list2 != null){//一旦有一个为空了就跳出,后面直接接上就好
            if (list1.val > list2.val) {
                res = list2;
                list2 = list2.next;
            }else {
                res = list1;
                list1 = list1.next;
            }
            res = res.next;
        }
        //循环结束开始处理多余链表的节点
        res = list1 != null ? list1 :list2;
        return res;
    }

非常果不其然的报错了,我已习惯我的逻辑总是有漏洞。跑了一遍测试用例,发现自己返回的是最后一个节点,于是又重新新建了一个虚拟头结点让res指向它,存储head便于后续返回头节点。但修改之后这个也是错的。

public static ListNode Merge(ListNode list1, ListNode list2) {
        //这里我开始思考要不要新建一个节点
        ListNode head = new ListNode(0);
        ListNode res = head;
        if (list1 == null && list2 == null)
            return null;
//        res = list1.val < list2.val ? list1 : list2;//先放一个值进去,这样后面循环里就直接res.next就好
        //这个不需要
        while (list1 != null && list2 != null){//一旦有一个为空了就跳出,后面直接接上就好
            if (list1.val > list2.val) {
                res = list2;
                list2 = list2.next;
            }else {
                res = list1;
                list1 = list1.next;
            }
            res = res.next;
        }
        //循环结束开始处理多余链表的节点
        res = list1 != null ? list1 :list2;
        return head.next;
    }

错误结果如下,返回的是一个为空的节点。
在这里插入图片描述
后面测试了一通,发现在代码运行的过程中,链表连接的节点都是没有错的,就是在返回的时候一直返回null,我想了一会发现还是虚拟头结点的问题,由于res直接获得的是head,head是虚拟头结点,应该在res.next开始存入值,但是我从res就开始存储值了。修改后的代码是这样的:

public static ListNode Merge(ListNode list1, ListNode list2) {
        //这里我开始思考要不要新建一个节点
        if (list1 == null && list2 == null)
            return null;
        ListNode head = new ListNode(0);
        ListNode res = head;
//        res = list1.val < list2.val ? list1 : list2;//先放一个值进去,这样后面循环里就直接res.next就好
        //这个不需要
        while (list1 != null && list2 != null){//一旦有一个为空了就跳出,后面直接接上就好
            if (list1.val > list2.val) {
//                res = list2;//我写的逻辑没错啊,在调试的时候存储的节点都是对的
                res.next = list2;//是因为要保留第一个节点好返回吗
                list2 = list2.next;
            }else {
                res.next = list1;
                list1 = list1.next;
            }
            res = res.next;
        }
        //循环结束开始处理多余链表的节点
        res.next = list1 != null ? list1 :list2;
        return head.next;
    }

测试通过
在这里插入图片描述
放上测试用例及构建用例代码

//这个是构建链表的测试用例
    public static class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

    private static ListNode createLinkedList(int[] arr) {// 将输入的数组输入到链表中
        if (arr.length == 0) {
            return null;
        }
        ListNode head = new ListNode(arr[0]);
        ListNode current = head;
        for (int i = 1; i < arr.length; i++) {// 过程
            current.next = new ListNode(arr[i]);
            current = current.next;
        }
        return head;
    }

    private static void printLinkedList(ListNode head) {// 将链表结果打印
        ListNode current = head;
        while (current != null) {
            System.out.printf("%d -> ", current.val);
            current = current.next;
        }
        System.out.println("NULL");
    }

    public static void main(String[] args) {
        int[] x = { 1, 3, 3, 4, 5, 8 };
        int[] y = { 0, 2, 3, 6, 7};
//        ListNode list = createLinkedList(x);
//        printLinkedList(list);
//        System.out.println(Merge(createLinkedList(x), createLinkedList(y)));
        printLinkedList(Merge(createLinkedList(x), createLinkedList(y)));
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值