合并两个有序的单链表,合并之后的链表依然有序

本文介绍了一种合并两个已排序单链表的方法,通过创建一个新的链表来存储两个链表的有序元素,实现了链表的高效合并。代码示例展示了如何在Java中实现这一算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:
新建一个单链表,每次都把两个有序链表中的更小的值加入到新链表中

public class LinkedListConbine {

    public static void main(String[] args) {
        SingleLinkedList4 singleLinkedList1 = new SingleLinkedList4();
        SingleLinkedList4 singleLinkedList2 = new SingleLinkedList4();

        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(5);
        Node node6 = new Node(6);
        Node node7 = new Node(7);
        Node node8 = new Node(8);

        singleLinkedList1.add(node3);
        singleLinkedList1.add(node1);
        singleLinkedList1.add(node7);
        singleLinkedList1.add(node5);
        singleLinkedList2.add(node2);
        singleLinkedList2.add(node6);
        singleLinkedList2.add(node8);
        singleLinkedList2.add(node4);

        System.out.println("链表一:");
        singleLinkedList1.show(singleLinkedList1.head);
        System.out.println("链表二:");
        singleLinkedList2.show(singleLinkedList2.head);

        System.out.println("合并两个链表==========");
        Node conbine = singleLinkedList1.conbine(singleLinkedList1.head, singleLinkedList2.head);
        singleLinkedList1.show(conbine);

    }
}

class SingleLinkedList4 {
    public Node head;

    public SingleLinkedList4() {
        head = new Node(-1);
    }

    /**
     * 数据按从小到大的顺序添加到链表中
     * @param newNode   新节点
     */
    public void add(Node newNode) {
        //辅助变量必须从head开始,不能从head.next开始,否则链表就断了
        Node cur = head;
        Node pre = head;
        int value = newNode.num;
        while (cur.next != null) {
            pre = cur;
            cur = cur.next;
            if(value <= cur.num) {  //小于节点cur,插在cur前面
                newNode.next = cur;
                pre.next = newNode;
                return; //当前链表中存在大于等于value的值
            }
        }
        cur.next = newNode; //当前链表中不存在大于等于value的值,则添加到末尾
    }

    public void show(Node head) {
        Node cur = head.next;
        if (cur == null) {
            System.out.println("链表为空~");
        }
        while (cur != null) {
            System.out.println(cur);
            cur = cur.next;
        }
    }

    public Node conbine(Node head1, Node head2) {
        Node conbine = new Node(-1);
        Node conbineTemp = conbine;
        Node cur1 = head1.next;
        Node cur2 = head2.next;
        while (cur1 != null && cur2 != null) {
            if (cur1.num <= cur2.num) {
                conbineTemp.next = new Node(cur1.num);
                conbineTemp = conbineTemp.next;
                cur1 = cur1.next;
            } else {
                conbineTemp.next = new Node(cur2.num);
                conbineTemp = conbineTemp.next;
                cur2 = cur2.next;
            }
        }
        if (cur1 == null) {
            while (cur2 != null) {
                conbineTemp.next = new Node(cur2.num);
                conbineTemp = conbineTemp.next;
                cur2 = cur2.next;
            }
        } else {
            while (cur1 != null) {
                conbineTemp.next = new Node(cur1.num);
                conbineTemp = conbineTemp.next;
                cur1 = cur1.next;
            }
        }
        return conbine;
    }
}

class Node {
    public int num;
    public Node next;

    public Node(int num) {
        this.num = num;
    }

    @Override
    public String toString() {
        return "Node{" +
                "num=" + num +
                '}';
    }
}

在这里插入图片描述

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值