Java 中的反向链表
我正在查看这篇文章中的解决方案 递归地反转 Java 中的链表< /a>
我复制了下面的解决方案之一。我已经实现了并且效果很好。
public ListNode Reverse(ListNode list)
{
if (list == null) return null; // first question
if (list.next == null) return list; // second question
// third question - in Lisp this is easy, but we don't have cons
// so we grab the second element (which will be the last after we reverse it)
ListNode secondElem = list.next;
// bug fix - need to unlink list from the rest or you will get a cycle
list.next = null;
// then we reverse everything from the second element on
ListNode reverseRest = Reverse(secondElem);
// then we join the two lists
secondElem.Next = list;
return reverseRest;
}
然而,我不明白的是最后几行。
secondElem.next = list;
return reverseRest;
看来我们根本没有返回第二个Elem?但我调试了代码,看起来第二个Elem已经在reverseRest里面了。这是因为它是 Java 中的值引用,并且在应用 secondElem.Next=list
时会自动更新吗?
I am looking at the solution in this post Reversing a linked list in Java, recursively
I copied the one of the solutions below. I've implemented it and it works fine.
public ListNode Reverse(ListNode list)
{
if (list == null) return null; // first question
if (list.next == null) return list; // second question
// third question - in Lisp this is easy, but we don't have cons
// so we grab the second element (which will be the last after we reverse it)
ListNode secondElem = list.next;
// bug fix - need to unlink list from the rest or you will get a cycle
list.next = null;
// then we reverse everything from the second element on
ListNode reverseRest = Reverse(secondElem);
// then we join the two lists
secondElem.Next = list;
return reverseRest;
}
What I do NOT understand is, however, is the last few lines.
secondElem.next = list;
return reverseRest;
It seems we are not returning the secondElem at all? But I debugged through the code and looks like secondElem is already inside reverseRest. Is this because it's a reference by value in Java and it's automatically updated when secondElem.Next=list
is applied?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要考虑传递语义,而是考虑内存中的对象和包含对它们的引用的变量。
初始状态:
list.next = null;
之后:递归调用之后:
之后的最终状态代码>secondElem.Next = list;
:Don't think about passing semantics, think in terms of objects in memory and variables containing references to them.
Initial state:
After
list.next = null;
:After recursive call:
Final state after
secondElem.Next = list;
:如果您的目标只是反转列表,请使用
Collections.reverse(list)
If your goal is to simply reverse the list, use
Collections.reverse(list)
让我一一解释一下你不理解的两行:
我认为第二个Elem的创建引入了额外的复杂性。根本不需要变量 secondaryElem。
代替这个:
使用这个:
更容易掌握。最后一行代码清楚地显示了列表反转过程本身!
看看reverseRest变量的用法&了解它的目的。它的唯一目的是将列表的原始尾部的值渗透到所有递归调用的末尾,以便我们可以将原始尾部(也是新的头)返回到调用 Reverse 函数的主函数。您必须快速理解这一点,因为我们根本没有在递归调用之间更改变量reverseRest的值 - 无论我们从 Reverse(secondElem) 收到什么返回值,我们都会从函数中“按原样”返回它!
Let me explain you the 2 lines that you did not understand one by one:
I think the creation of secondElem introduces additional complexity. The variable secondElem is not needed at all.
Instead of this:
Use this:
It is easier to grasp. The last line of code clearly shows the list reversal process in itself!
Look at the usage of reverseRest variable & understand it's purpose. It's only purpose is to percolate the value of the original tail of the list to the end of all the recursion calls, so that we can return the original tail (which is also the new head) to the main function that called the Reverse function. You must quickly get this by understanding that we are not at all changing the value of variable reverseRest in between the recursion calls - whatever we receive as return value from Reverse(secondElem) we are returning it "as it is" from the function!