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

package linkedlist;
/**
 * @Description: 单向链表
 * @Author: li
 * @Create: 2020-01-29 14:40
 */
public class SingleLinkedListDemo {
    public static void main(String[] args) {
        PersonNode node1 = new PersonNode(1, "li");
        PersonNode node2 = new PersonNode(2, "wang");
        PersonNode node3 = new PersonNode(3, "zhao");
        PersonNode node4 = new PersonNode(4, "zhao1");
        PersonNode node5 = new PersonNode(5, "zhao2");
        PersonNode node6 = new PersonNode(6, "zhao26");
        PersonNode node7 = new PersonNode(7, "zhao2677");
        PersonNode node8 = new PersonNode(8, "zhao2688");

        SingleLinkedList list3 = new SingleLinkedList();
        list3.addNodeByNo(node3);
        list3.addNodeByNo(node1);
        list3.addNodeByNo(node5);
        list3.addNodeByNo(node8);
        SingleLinkedList list4 = new SingleLinkedList();
        list4.addNodeByNo(node4);
        list4.addNodeByNo(node2);
        list4.addNodeByNo(node6);
        list4.addNodeByNo(node7);


        list3.showNode();
        System.out.println("============");
        list4.showNode();

        System.out.println("合并");
        SingleLinkedList list5 = list3.merge(list3, list4);
        list5.showNode();
    }
}

class SingleLinkedList {
    //定义头结点
    PersonNode head;

    public SingleLinkedList(PersonNode head) {
        this.head = head;
    }

    public SingleLinkedList() {
        head = new PersonNode();
    }

    //按照编号 添加节点
    public void addNodeByNo(PersonNode node) {
        PersonNode temp = head;

        //存在编号相同
        boolean flag = false;

        while (true) {
            //后移 要判断是否为最后一个节点
            if (temp.next == null) {
                //到达最后一个节点
                break;
            }

            if (temp.next.no > node.no) {
                //找到插入的位置
                break;
            } else if (temp.next.no == node.no) {
                //编号相同
                flag = true;
                break;
            }
            //后移
            temp = temp.next;
        }

        if (flag) {
            System.out.println("编号以存在");
        } else {
            //插入新节点
            node.next = temp.next;
            temp.next = node;
        }
    }

    //遍历节点
    public void showNode() {
        //判断链表是否为空
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }

        //定义一个辅助节点
        PersonNode temp = head.next;

        while (true) {
            if (temp == null) {
                break;
            }
            //不是最后一个节点,一直下移
            System.out.println(temp);
            temp = temp.next;
        }
    }

    //合并两个有序的单链表,合并之后的链表依然有序
    public SingleLinkedList merge(SingleLinkedList list1, SingleLinkedList list2) {
        if (list1.head.next == null) {
            return list2;
        } else if (list2.head.next == null) {
            return list1;
        }

        PersonNode newNode = new PersonNode();
        PersonNode n1 = newNode;
        PersonNode l1 = list1.head.next;
        PersonNode l2 = list2.head.next;

        while (l1 != null && l2 != null) {
            if (l1.no < l2.no) {
                n1.next = l1;
                l1 = l1.next;
                n1 = n1.next;
            } else {
                n1.next = l2;
                l2 = l2.next;
                n1 = n1.next;
            }
        }

        if (l1 == null) {
            n1.next = l2;
        }
        if (l2 == null) {
            n1.next = l1;
        }
        return new SingleLinkedList(newNode);
    }


}

class PersonNode {
    int no;
    String name;
    PersonNode next;

    public PersonNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public PersonNode() {
    }


    @Override
    public String toString() {
        return "PersonNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值