Java合并两个单链表,要求合并后的链表保持有序

课程:尚硅谷 韩顺平老师《数据结构》

语言:Java

课后练习题目要求: 合并两个单链表,且合并后的链表仍保持有序。

题解:

public class withTwoLinkedList {
    public static void main(String[] args){
        Node node1 = new Node(1,"jack");
        Node node2 = new Node(3,"mike");
        Node node3 = new Node(7,"amy");
        Node node4 = new Node(2,"zoey");
        Node node5 = new Node(6,"bob");
        Node node6 = new Node(9,"tom");

        SingleLinkedList singleLinkedList1 = new SingleLinkedList();
        SingleLinkedList singleLinkedList2 = new SingleLinkedList();

        singleLinkedList1.addByOrder(node1);
        singleLinkedList1.addByOrder(node2);
        singleLinkedList1.addByOrder(node3);


        singleLinkedList2.addByOrder(node4);
        singleLinkedList2.addByOrder(node5);
        singleLinkedList2.addByOrder(node6);

        System.out.println("第一个链表:\n");
        singleLinkedList1.list();
        System.out.println("第二个链表:\n");
        singleLinkedList2.list();

        with2list(singleLinkedList1.getHead(),singleLinkedList2.getHead());
        System.out.println("合并后的链表是");
        singleLinkedList2.list();

    }
    public static Node with2list(Node head1,Node head2){
        if (head1.next == null && head2.next ==null){
            return null;
        }
        if (head1.next == null){
            return head2;
        }
        if (head2.next == null){
            return head1;
        }
        Node head = null;
        if (head1.no > head2.no) {
            //把head较小的结点给头结点
            head = head2;
            //继续递归head2
            head.next = with2list(head1, head2.next);
        } else {
            head = head1;
            head.next = with2list(head1.next, head2);
        }

        return head;
    }
}

class SingleLinkedList {
    private Node head = new Node(0, "");

    public Node getHead() {
        return head;
    }

    public void addByOrder(Node node){
        Node 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.printf("欲插入的编号%d 已经存在不能加入\n",node.no);
        }else {
            node.next = temp.next;
            temp.next = node;

        }
    }

    //显示链表
    public void list(){
        //判断链表是否为空
        if(head.next == null){
            System.out.println("链表为空");
            return;
        }
        Node temp = head.next;
        while(true){
            if (temp == null){
                break;
            }
            System.out.println(temp);
            temp = temp.next;//一定记住temp后移不然是一个死循环。
        }
    }

}

class Node{
    public int no;
    public String data;
    public Node next;
    public Node(int no,String data){
        this.no = no;
        this.data = data;

    }
    @Override
    public String toString(){
        return "Node [no=" + no + ",data="+ data + "]";
    }
}

编程小白,数据结构真滴好好玩哦(*^▽^*),希望在暑假学完这门课程~有问题请指教!

Java合并两个单链表,要求合并后的链表保持有序_第1张图片

 

你可能感兴趣的:(数据结构,链表,java)