簡體   English   中英

合並鏈接列表中的遞歸

[英]Recursion In Merging Linked Lists

我開始理解遞歸,

我已經附上了遞歸代碼以合並兩個排序的鏈表,

我的問題是,我了解到“ temp”會在第一個或第二個為空時返回temp->(第一(或)第二)的值,但是我無法理解以下事實:

假設我有5-> 10-> 15-> 20。

最終函數返回15-> 20,然后將其組合為root.next-> temp,但是在我返回temp的那一步之后,為什么返回根值。 即10-> 15-> 20,當我希望只返回溫度時。

請找到代碼,

 /**
 * 
 */
 *
 *
 */
public class MergeLinkedLists {

    static class Node {

        int data;
        Node next;

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

    }

    Node root;

        /**
     * @param args
     */

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        MergeLinkedLists s1 = new MergeLinkedLists();
        s1.root = new Node(0);
        Node n1 = new Node(10);
        //n1.next = new Node(20);
        //n1.next.next = new Node(30);

        Node n2 = new Node(5);
        n2.next = new Node(15);
        //n2.next.next = new Node(50);

        Node result = sortedLists(n1, n2, s1.root);

        while (result != null) {
            System.out.print(result.data + "--->");
            result = result.next;
        }
    }

    /**
     * @param n1
     * @param n2
      * @param root2
     */
     private static Node sortedLists(Node n1, Node n2, Node root) {
         // TODO Auto-generated method stub

        Node temp = root;

        Node first = n1; // 10 20 30
        Node second = n2; // 5 15 50

        if (first == null) {
            temp.next = second;
            return temp;
        } else if (second == null) {
            temp.next = first;
            return temp;
        }

        else if (first.data < second.data) {
            temp = new Node(first.data);
            first = first.next;
        } else {
            temp = new Node(second.data);
            second = second.next;
        }

        sortedLists(first, second, temp);
        root.next = temp;
        System.out.println("The Temp Data is ::::"+temp.data);
        return temp;

    }

}

因為在這種情況下, temp 根值。 不用擔心,這是您需要了解遞歸本身的一件事。

理解代碼功能的一個重要功能就是使用調試器。 在函數調用中設置一個斷點,就可以逐步執行程序,同時可以觀察變量的變化。

除此之外,讓我們看一下您的代碼。

需要注意的重要一點是,每當您調用sortedLists(first, second, temp); 指針將進入它,直到函數終止(因此當它返回temp時)。 隨着程序的繼續,您將多次調用此函數,因此它將更深入地了解其自身的功能。 然后,它將逐步逐步攜帶信息,直到第一次調用sortedLists(first, second, temp);為止sortedLists(first, second, temp); 終止,這在您的主要方法中Node result = sortedLists(n1, n2, s1.root);

我將嘗試與您的示例相處:

  1. Node result = sortedLists(n1, n2, s1.root); main()方法中調用。 因此根為0,您的節點為10和5,15。

  2. 在第一次調用sortedLists() temp變為5, second現在攜帶15,因為first.data > second.data

  3. 現在,您再次調用sortedLists()方法,但使用新值:現在根是5,第一個仍然是10,第二個是15。我們進入函數並從新值開始:

    1. 由於first.data < second.data ,temp變為10,first現在為null。
    2. 一次又一次:我們在sortedLists()上達到了另一個調用。 我們將值傳遞給該函數: first = null second = 15 root = 10 ,然后再深入一步。
      1. 因為first現在是零, temp.next將是second (15)
      2. temp現在看起來像這樣:10-> 15,我們返回。 (這是第一次sortedLists()終止!)
    3. 我們現在回到這里,可以繼續使用root.next = temp; 還記得這種情況下的溫度嗎? temp是10,但已通過上述功能進行了更新。 新的temp是10-> 15,所以我們的root看起來像是:5-> 10-> 15。
    4. 然后打印temp的開頭,即10。此功能也會終止。
  4. 現在我們回到第一個調用中,看看會發生什么:在3.3中,我們更新了它的根。 3.3的根是3的溫度,因為我們調用了sortedLists(first, second, temp) (temp用作根,因為此函數的最后一個參數是Node root )。

  5. 總結:我們的根仍然是0,溫度是5-> 10-> 15

  6. root.next在此分配后為0-> 5-> 10-> 15。 現在,您在類中的main方法上方聲明的根具有值。

  7. 我們返回的溫度是5-> 10-> 15,我們完成了。

現在,我們可以繼續main方法,打印結果。

要在沒有result.next時擺脫--->,可以像這樣處理null值:

 while (result != null) {
            if (result.next != null)
            System.out.print(result.data + "--->");
            else System.out.println(result.data);
            result = result.next;
        }

我希望這有助於遞歸。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM