哪个是在递归递归链接列表时首先出现的?

发布于 2025-02-05 02:26:08 字数 298 浏览 2 评论 0原文

因此,我正在Java学习DSA,并扭转了链接列表。我理解了迭代方法,但是在学习递归方法的同时,递归有两件事的时间点:

  1. 将链接转换
  2. 为列表底部的链接并确定新的Head

Q1。哪个是第一个?(请一步一步) 堆栈被穿过底部,在上升时,堆栈会逆转链接,并返回新的头部,或者链接在其底部和到达时逆转,将新的头部返回到顶部。

Q2。如何将新节点返回到上方的递归(请逐步)?

如果我理解错误的话,有人可以向我解释递归如何深入起作用吗?不仅是说将其留给信仰的递归飞跃。我想完整地理解它及其步骤。

So, I was learning DSA in java and came to reversing a linked list. I understood the iterative method, but while learning the recursive method there was a point in time where the recursion does two things :

  1. reverse the links
  2. traverse to the bottom of the list and determine the new head

Q1. Which comes first?(step by step please)
The stack is traversed to the bottom and while on its way up reverses the links and returns the new head OR The link is reversed on its way bottom and when reached, returns the new head to the top.

Q2. How does the new node be returned to the recursion above it(step by step please)?

If I'm understanding it wrong can someone please explain it to me how the recursion works in depth? not just by stating leave it to recursive leap of faith. I want to understand it in full depth and its steps.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我喜欢麦丽素 2025-02-12 02:26:08
  • Q1:将列表横穿到末端,并在堆栈上保留反向链接,将新的头部返回到顶部。 (另一种方式也可以参见下面的输出后面。)

  • Q2:这个想法始终是返回新的头部,但不要立即返回。从递归呼叫返回后,先开关方向(利用当前节点的堆栈上维护的信息),然后返回(通过)新头。线索是在堆栈上放置两个引用,以便可以为每个列表元素恢复链接方向。

reverselist.java

class ListElement {
    String name;
    ListElement next;
    ListElement(String name, ListElement next) {
        this.name = name;
        this.next = next;
    }
}

 public class ReverseList {
    static ListElement reverseList(ListElement previous, ListElement node) {
        if (node == null) { // reached end directly behind last node
            return previous; // new list head
        } else {
            ListElement newHead = reverseList(node, node.next);
            node.next = previous; // turn link direction
            return newHead; // pass through new list head
        }
    }

    static void printList(ListElement list) {
        while (list != null) {
            System.out.print(list.name + " -> ");
            list = list.next;
        }    
        System.out.println("null");
    }

    public static void main(String args[]) {
        ListElement c = new ListElement("c", null);
        ListElement b = new ListElement("b", c);
        ListElement a = new ListElement("a", b);

        ListElement head = a;
        printList(head);
        head = reverseList(null, head); // new list end and head of list
        printList(head);
    }
}

让我们看看我们得到的是:

$ javac ReverseList.java
$ java ReverseList      
a -> b -> c -> null
c -> b -> a -> null
$  

在逐步下降时也可以转动链接方向:

        else {
            ListElement tmp = node.next;
            node.next = previous; // turn link direction
            return reverseList(node, tmp); // pass through new list head
        }

但是,我更喜欢在修改列表之前先先到达末端(底部)。在堆栈溢出的情况下,这会防止断裂的链接。

  • Q1: Traverse the list to the end and preserve backlink on stack, return new head to the top. (The other way is also possible see below behind the output.)

  • Q2: The idea is always to return the new head but not to return it immediately. After returning from a recursive call first switch the direction (utilizing information maintained on the stack for the current node) and then return (pass through) the new head. The clue is to put two references on the stack so that the link direction can be reverted for each list element.

ReverseList.java

class ListElement {
    String name;
    ListElement next;
    ListElement(String name, ListElement next) {
        this.name = name;
        this.next = next;
    }
}

 public class ReverseList {
    static ListElement reverseList(ListElement previous, ListElement node) {
        if (node == null) { // reached end directly behind last node
            return previous; // new list head
        } else {
            ListElement newHead = reverseList(node, node.next);
            node.next = previous; // turn link direction
            return newHead; // pass through new list head
        }
    }

    static void printList(ListElement list) {
        while (list != null) {
            System.out.print(list.name + " -> ");
            list = list.next;
        }    
        System.out.println("null");
    }

    public static void main(String args[]) {
        ListElement c = new ListElement("c", null);
        ListElement b = new ListElement("b", c);
        ListElement a = new ListElement("a", b);

        ListElement head = a;
        printList(head);
        head = reverseList(null, head); // new list end and head of list
        printList(head);
    }
}

Let's see what we got:

$ javac ReverseList.java
$ java ReverseList      
a -> b -> c -> null
c -> b -> a -> null
$  

It is also possible to turn the link direction while stepping down:

        else {
            ListElement tmp = node.next;
            node.next = previous; // turn link direction
            return reverseList(node, tmp); // pass through new list head
        }

Nevertheless, I prefer to reach the end (bottom) first before modifying the list. This prevents broken linkage in case of a stack overflow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文