哪个是在递归递归链接列表时首先出现的?
因此,我正在Java学习DSA,并扭转了链接列表。我理解了迭代方法,但是在学习递归方法的同时,递归有两件事的时间点:
- 将链接转换
- 为列表底部的链接并确定新的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 :
- reverse the links
- 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Q1:将列表横穿到末端,并在堆栈上保留反向链接,将新的头部返回到顶部。 (另一种方式也可以参见下面的输出后面。)
Q2:这个想法始终是返回新的头部,但不要立即返回。从递归呼叫返回后,先开关方向(利用当前节点的堆栈上维护的信息),然后返回(通过)新头。线索是在堆栈上放置两个引用,以便可以为每个列表元素恢复链接方向。
reverselist.java
让我们看看我们得到的是:
在逐步下降时也可以转动链接方向:
但是,我更喜欢在修改列表之前先先到达末端(底部)。在堆栈溢出的情况下,这会防止断裂的链接。
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
Let's see what we got:
It is also possible to turn the link direction while stepping down:
Nevertheless, I prefer to reach the end (bottom) first before modifying the list. This prevents broken linkage in case of a stack overflow.