简体   繁体   English

递归链表反向器

[英]Recursive Linked List Reverser

Created a linked list class for a java project and needed a method to reverse the list so my friend and I eventually came up with the following recursive method and it's helper method: 为一个Java项目创建了一个链表类,并且需要一个方法来反转该列表,所以我和我的朋友最终提出了以下递归方法及其辅助方法:

(just for clarification, first is the instance variable of the first node in the list) (为澄清起见, first是列表中第一个节点的实例变量)

public void reverse()
{
    first = reverse(first);
}

public Node reverse(Node in) 
{
    if (in == null || in.next == null) 
    {
         return in;
    }

         Node next = in.next;
         in.next = null;
         Node rest = reverse(next);
         next.next = in;
         return rest;
}

When the nodes contain integers and we input 1, 2, and 3 at the beginning of the list (they become 3, 2, 1 when added as the first node) and then try to reverse the 3, 2, 1 to become 1, 2, 3 the function works as planned. 当节点包含整数并且我们在列表的开头输入1、2和3(当添加为第一个节点时,它们分别变为3、2、1),然后尝试将3、2、1反转为1。 2、3功能按计划工作。

My issue here is that I really am having trouble comprehending what is happening here. 我的问题是,我真的很难理解这里发生的事情。

For instance, when I trace the function (for 3, 2, 1) it appears that: 例如,当我跟踪函数(对于3、2、1)时,似乎:

next = in.next (which is 2, 1 ) gets passed down again until it is just (1) and then that gets passed back up into rest . next = in.next2, 1 )再次向下传递,直到只是(1) ,然后再传递回rest But after the recursion takes place, nothing is actually happening to rest . 但递归发生之后,没有什么是实际发生于rest It's happening to next.next and I cannot make out how next.next is affecting rest in any way. next.next正在发生,我无法确定next.next如何以任何方式影响rest

I can't seem to locate the points in which rest goes from being (1) to (1,2) to (1,2,3) during the bubbling back up. 我似乎找不到在鼓泡备份期间rest(1)变为(1,2)(1,2,3)的点。

If anyone could help explain more in depth how this function is modifying rest that would be great. 如果有人可以帮助您更深入地解释此功能如何修改rest ,那将是很好的。 I have traced this function almost 20 or so times now trying to understand and something is taking place that I just can't seem to see. 我已经尝试了近20次左右来尝试了解此功能,并且正在发生一些似乎看不到的事情。

I even took it to my teacher and he gave me some BS response equivalent to "If it works, then it works, it's recursion, don't try to understand it." 我什至把它带给了我的老师,他给了我一些BS答复,相当于“如果有效,那么有效,这是递归,不要试图去理解它。” -- What kind of advice is that?? -那是什么建议?

Thanks 谢谢

Try drawing a list with 2 nodes in it. 尝试绘制其中有2个节点的列表。 This is the simplest example. 这是最简单的例子。

first --> [A] -> [B] -> null

We invoke reverse(first) , and since A is not-null, we result in the following assignments: 我们调用reverse(first) ,并且由于A不为空,因此导致以下分配:

in = A
next = B

We break the link between A and B at this step: 在这一步,我们断开A和B之间的链接:

in.next = null;

So, we have: 因此,我们有:

first --> [A]  [B] -> null

Now we want the rest, which gets assigned to a recursive call of this function, with B instead. 现在我们想要其余的,然后分配给该函数的递归调用,用B代替。 We repeat the steps now with B, but since B.next is null, we're only left with B. 现在,我们对B重复上述步骤,但是由于B.next为空,因此只剩下B了。

So, we get B back as the result of rest . 因此,我们得到B作为rest的结果。 We now assign next.next to in , which means that B.next is now A . 现在我们将next.next分配给in ,这意味着B.next现在是A

[B] -> [A] -> null
        ^
      first

We return rest back to whomever called it, which in this case, results in the assignment to first . 我们将rest归还给调用它的任何人,在这种情况下,这导致分配给first

first --> [B] -> [A] -> null

...and now, the list is reversed. ...现在,该列表被颠倒了。

This is the general breakdown of it; 这是它的一般分解。 take a look at it with three nodes and you'll see a bit more behavior. 看一下三个节点,您会发现更多的行为。 Remember: each recursive call creates new variables for in and next , and the ones that existed previously are set aside until we return from this particular method. 请记住:每个递归调用都会为innext创建新变量,并且将先前存在的变量搁置一旁,直到我们从该特定方法返回为止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM