合併兩個有序的單鏈表,合併後的鏈表依然有序(java)

話不多說,直接上代碼

public class SignleLinkList {
    public static void main(String[] args) {
        Node node1 = new Node(2);
        Node node3 = new Node(3);
        node1.next = node3;


        Node node2 = new Node(1);
        Node node4 = new Node(4);
        Node node5 = new Node(5);

        node2.next = node4;
        node4.next = node5;

        System.out.println("單鏈表1");
        print(node1);
        System.out.println("單鏈表2");
        print(node2);
        System.out.println("合併後的單鏈表");
        print(merge(node1,node2));

    }

    /**
     * 合併有序單鏈表
     * @param node1
     * @param node2
     * @return
     */
    public static Node merge(Node node1,Node node2){
        Node cur1 = node1;
        Node cur2 = node2;
        Node temp = new Node(0);
        Node cur = temp;
        while (true){
            if(cur1 == null){
                cur.next = cur2;
                break;
            }
            if(cur2 == null){
                cur.next = cur1;
                break;
            }
            if(cur1.data<cur2.data){
                cur.next = cur1;
                cur = cur1;
                cur1 = cur1.next;
            }else{
                cur.next = cur2;
                cur = cur2;
                cur2 = cur2.next;
            }
        }
        return temp.next;
    }

    /**
     * 打印鏈表
     * @param head
     */
    public static void print(Node head){
        Node temp = head;
        while (temp != null){
            System.out.println(temp);
            temp = temp.next;
        }
    }

    static class Node{
        public int data;
        public Node next;

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

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

測試結果打印如下:
測試結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章