合并两个有序单链表

该博客介绍了如何使用Java实现合并两个有序单链表的方法。通过定义CustomLinkList类,包括头指针、当前指针和Node数据结构,实现了添加数据、合并数据和打印链表的功能。在mergeData方法中,比较两个链表节点的值,依次合并成新的有序链表,并处理剩余元素。测试方法展示了如何创建和合并两个链表,并输出合并后的结果。
摘要由CSDN通过智能技术生成
题目
  • 合并两个有序单链表
解法
  • 1、定义数据结构CustomLinkList
class CustomLinkList {
    // 头指针
    public Node head;
    // 当前指针
    public Node current;

    class Node {
        // 指针
        Node next;
        // 值
        int value;

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

    // 添加数据
    public void addData(int data) {
        // 空栈
        if (head == null) {
            head = new Node(data);
            current = head;
        } else {
            current.next = new Node(data);
            current = current.next;
        }
    }


    // 合并数据
    public Node mergeData(Node nodeOne, Node nodeTwo) {
        if (nodeOne == null && nodeTwo == null) {
            return null;
        }
        if (nodeOne == null) {
            return nodeTwo;
        }
        if (nodeTwo == null) {
            return nodeOne;
        }

        Node head;
        Node current;

        // 设置头节点
        if (nodeOne.value < nodeTwo.value) {
            head = nodeOne;
            current = nodeOne;
            nodeOne = nodeOne.next;
        } else {
            head = nodeTwo;
            current = nodeTwo;
            nodeTwo = nodeTwo.next;
        }

        // 合并元素
        while (nodeOne != null && nodeTwo != null) {
            if (nodeOne.value < nodeTwo.value) {
                current.next = nodeOne;
                current = current.next;
                nodeOne = nodeOne.next;
            } else {
                current.next = nodeTwo;
                current = current.next;
                nodeTwo = nodeTwo.next;
            }
        }

        // 合并剩余的元素
        if (nodeOne != null) {
            current.next = nodeOne;
        }
        if (nodeTwo != null) {
            current.next = nodeTwo;
        }
        return head;
    }
    
   // 打印链表
    public void printData(Node head) {
        Node node = head;
        while (node != null) {
            System.out.print("node is : " + node.data + "   ");
            node = node.next;
        }
        System.out.println();
    }

}
  • 2、提供测试方法
  private static void methodLink() {
        CustomLinkList listOne = new CustomLinkList();
        CustomLinkList listTwo = new CustomLinkList();
        // 添加数据
        for (int i = 0; i < 3; i++) {
            listOne.addData(i);
        }
        for (int i = 2; i < 5; i++) {
            listTwo.addData(i);
        }
        CustomLinkList listThree = new CustomLinkList();
        listThree.head = listThree.mergeData(listOne.head, listTwo.head);
        listThree.printData(listThree.head);
    }
  • 3、日志输出
node is : 1
node is : 2
node is : 2
node is : 3
node is : 4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值