合并两个排序后的链表

算法描述:
输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。

思路:
1. 两条链表list1+list21分别单调递增;
2. 选择较小的链表头作为合并后的头结点;
3. 再两两进行比较,知道两节点大小关系反转;
4. 时间复杂度O(n),空间复杂度O(1);
5. helper1程序看起来简单,但其中的每个节点要比较两次,不建议使用,建议helper2;
6. helper3通过牛客网上的test case,但是不能通过这里的case,wrong。放这里供参考;
7. 这里的程序copy下来完全可以跑;

public class Main {
    public static void main(String[] args) {
        ListNode list1 = new ListNode(1), list2 = new ListNode(2), list3 = new ListNode(3), list4 = new ListNode(7),
                list5 = new ListNode(9);
        list1.next = list2;
        list2.next = list3;
        list3.next = list4;
        list4.next = list5;

        ListNode head = list1;
        while (head != null) {
            System.out.print(head.val + " ");
            head = head.next;
        }
        System.out.println(" ");

        ListNode list21 = new ListNode(0), list22 = new ListNode(4), list23 = new ListNode(5), list24 = new ListNode(6),
                list25 = new ListNode(10);
        list21.next = list22;
        list22.next = list23;
        list23.next = list24;
        list24.next = list25;

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

        head = Merge(list1, list21);
        while (head != null) {
            System.out.print(head.val + " ");
            head = head.next;
        }
        System.out.println(" ");

    }
    public static ListNode Merge(ListNode list1, ListNode list2) {
        if (list1 == null)
            return list2;
        if (list2 == null)
            return list1;
        if (list1.val < list2.val)
            return helper1(list1, list2);
        return helper1(list2, list1);
    }

    public static ListNode helper1(ListNode head1, ListNode head2) {
        ListNode head = head1, next = null;
        while (head1 != null && head2 != null) {
            if (head1.val <= head2.val) {
                if (head1.next != null && head1.next.val <= head2.val) {
                    head1 = head1.next;
                    continue;
                }
                next = head1.next;
                head1.next = head2;
                head1 = next;
            } else {
                if (head2.next != null && head2.next.val < head1.val) {
                    head2 = head2.next;
                    continue;
                }
                next = head2.next;
                head2.next = head1;
                head2 = next;
            }
        }

        return head;
    }

    public static ListNode helper2(ListNode head1, ListNode head2) {
        ListNode head = head1, next = null;
        while (head1 != null && head2 != null) {
            if (head1.val <= head2.val) {
                while (head1.next != null && head2 != null) {
                    if (head1.next.val <= head2.val) {
                        head1 = head1.next;
                    } else
                        break;
                }
                next = head1.next;
                head1.next = head2;
                head1 = next;
            } else {
                while (head1 != null && head2.next != null) {
                    if (head1.val > head2.next.val) {
                        head2 = head2.next;
                    } else
                        break;
                }
                next = head2.next;
                head2.next = head1;
                head2 = next;
            }
        }

        return head;
    }

    public static ListNode helper3(ListNode head1, ListNode head2) {
        ListNode head = head1, next1 = null, next2 = null;
        while (head1 != null && head2 != null) {
            if (head1.val <= head2.val) {
                next1 = head1.next;
                head1.next = head2;
                head1 = next1;
            } else {
                next2 = head2.next;
                head2.next = head1;
                head2 = next2;
            }
        }
        return head;
    }
   }

    class ListNode {
        int val;
        ListNode next = null;
        ListNode(int val) {
            this.val = val;
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值