算法题_两个有序列表的合并

一、题目分析

在这里插入图片描述

1.1 mergeTwoLists 主程序

在这里插入图片描述

  1. 首先确定最小值为新的头节点。
  2. 定义两个当前值 cur1cur2,定义pre为当前值的上一位元素。当两个当前值都不为null时,cur1cur2谁最小,就确定为当前值,将prenext指针指向它。然后该当前值向下一位,另一个当前值不变,同时,pre来到当前值的位置。
  3. cur1cur2至少有一个为null时,结束循环,将prenext指向另一个不为null的链表,结束。

二、代码实现

package com.lsh.day05;

/**
 * @author :LiuShihao
 * @date :Created in 2022/2/7 1:51 下午
 * @desc :两个有序链表的合并
 */
public class Code07 {

    public static class ListNode{
        public int value;
        public ListNode next;

        public ListNode(int v){
            value = v;
            next = null;
        }
    }

    /**
     * 合并两个有序两表
     * @param head1
     * @param head2
     * @return
     */
    public static ListNode mergeTwoLists(ListNode head1,ListNode head2){
        //边界条件 如果head1为null则返回head2;head2位null返回head1
        if (head1 == null || head2 == null){
            return head1 == null ? head2 : head1;
        }
        //确定头节点,值小的为头节点
        ListNode head = head1.value < head2.value ? head1 : head2;
        // cur1 为头节点的下一位
        ListNode cur1 = head.next;
        // cur2 为另一个链表的头节点
        ListNode cur2 = head == head1 ? head2 : head1;
        // 定义pre为当前节点的上一个节点
        ListNode pre = head;
        //cur1和cur2都不为空
        while (cur1 != null && cur2 != null){
            if (cur1.value <= cur2.value){
                pre.next = cur1;
                //此时pre来到 cur1
                //cur1 来到下一位
                cur1 = cur1.next;
            }else {
                //head1.value > head2.value
                pre.next = cur2;
                cur2= cur2.next;
            }
            //pre 向下移动一位
            pre = pre.next;
        }
        //cur1和cur2有一个为空 或者两个都为空
        pre.next = cur1 == null ? cur2 : cur1;
        return head;
    }

    /**
     * 1 -> 3 -> 5 -> 7 -> 9
     * 2 -> 4 -> 6 -> 8 -> 10 -> 12 -> 14
     * @param args
     */
    public static void main(String[] args) {
        ListNode head1 = new ListNode(1);
        head1.next = new ListNode(3);
        head1.next.next = new ListNode(5);
        head1.next.next.next = new ListNode(7);
        head1.next.next.next.next = new ListNode(9);

        ListNode head2 = new ListNode(2);
        head2.next = new ListNode(4);
        head2.next.next = new ListNode(6);
        head2.next.next.next = new ListNode(8);
        head2.next.next.next.next = new ListNode(10);
        head2.next.next.next.next.next = new ListNode(12);
        head2.next.next.next.next.next.next = new ListNode(14);
        ListNode head = mergeTwoLists(head1, head2);
        while (head != null){
            System.out.print(head.value + " -> ");
            head = head.next;
        }
        System.out.println();
    }
}

三、程序验证

在这里插入图片描述

 /**
     * 1 -> 3 -> 5 -> 7 -> 9
     * 2 -> 4 -> 6 -> 8 -> 10 -> 12 -> 14
     * @param args
     */
    public static void main(String[] args) {
        ListNode head1 = new ListNode(1);
        head1.next = new ListNode(3);
        head1.next.next = new ListNode(5);
        head1.next.next.next = new ListNode(7);
        head1.next.next.next.next = new ListNode(9);

        ListNode head2 = new ListNode(2);
        head2.next = new ListNode(4);
        head2.next.next = new ListNode(6);
        head2.next.next.next = new ListNode(8);
        head2.next.next.next.next = new ListNode(10);
        head2.next.next.next.next.next = new ListNode(12);
        head2.next.next.next.next.next.next = new ListNode(14);
        ListNode head = mergeTwoLists(head1, head2);
        while (head != null){
            System.out.print(head.value + " -> ");
            head = head.next;
        }
        System.out.println();
    }
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Liu_Shihao

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

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

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

打赏作者

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

抵扣说明:

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

余额充值