1. 程式人生 > 其它 >連結串列-合併兩個有序的單鏈表,合併之後依然有序-5

連結串列-合併兩個有序的單鏈表,合併之後依然有序-5

參考連結:

https://blog.csdn.net/zhangquan2015/article/details/82818614

/**
 * 定義一個Node節點
 */
class Node {
    Node next = null;
    int data;

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

    @Override
    public String toString() {
        return super.toString();
    }
}

public class Test14_合併兩個連結串列 {


    public static void main(String[] args) {
        Node head1 = new Node(1);//1->3->5
        Node node3 = new Node(3);
        head1.next = node3;
        Node node5 = new Node(5);
        node3.next = node5;
        node5.next = null;

        Node head2 = new Node(2);//2->4->6
        Node node4 = new Node(4);
        head2.next = node4;
        Node node6 = new Node(6);
        node4.next = node6;
        node6.next = null;

//        Node mergeHead = mergeList(head1, head2);
//        while (mergeHead != null) {
//            System.out.print(mergeHead.data + " ");
//            mergeHead = mergeHead.next;//1->2->3->4->5->6
//        }

        Node mergeHead2 = mergeList2(head1, head2);
        while (mergeHead2 != null) {
            System.out.print(mergeHead2.data + " ");
            // mergeHead2.next  後移 遍歷
            mergeHead2 = mergeHead2.next;//1->2->3->4->5->6
        }
    }


    /**
     * https://blog.csdn.net/zhangquan2015/article/details/82818614
     * 使用遞迴
     *
     * @param head1
     * @param head2
     * @return
     */
    public static Node mergeList(Node head1, Node head2) {
        if (head1 == null)
            return head2;
        if (head2 == null)
            return head1;
        Node head = null;
        if (head1.data < head2.data) {
            head = head1;
            head.next = mergeList(head1.next, head2);
        } else {
            head = head2;
            head.next = mergeList(head1, head2.next);
        }
        return head;
    }


    /**
     * 使用頭結點
     *
     * @param head1
     * @param head2
     * @return
     */
    public static Node mergeList2(Node head1, Node head2) {
        // 虛擬一個head節點
        Node finalRes = new Node(-1);
        Node res = finalRes;
        while (head1 != null & head2 != null) {
            if (head1.data > head2.data) {
                res.next = head2;
                head2 = head2.next;
                res = res.next;
            } else {
                res.next = head1;
                head1 = head1.next;
                res = res.next;
            }
            System.out.println();
        }
        if (head1 == null) {
            res.next = head2;
        }
        if (head2 == null) {
            res.next = head1;
        }
        return finalRes.next;
    }


}