Java 中的反向链表

发布于 2024-09-29 21:27:22 字数 1076 浏览 4 评论 0原文

我正在查看这篇文章中的解决方案 递归地反转 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 技术交流群。

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

发布评论

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

评论(3

暗地喜欢 2024-10-06 21:27:22

不要考虑传递语义,而是考虑内存中的对象和包含对它们的引用的变量。

初始状态:

(list)
   |
   V
[  1  ] -> [  2  ] -> ... -> [  N  ]

list.next = null;之后:

(list)   (secondElem)
   |          |
   V          V
[  1  ]    [  2  ] -> ... -> [  N  ]

递归调用之后:

(list)   (secondElem)      (reverseRest)
   |          |                 |
   V          V                 V
[  1  ]    [  2  ] <- ... <- [  N  ]

之后的最终状态代码>secondElem.Next = list;:

(list)   (secondElem)      (reverseRest)
   |          |                 |
   V          V                 V
[  1  ] <- [  2  ] <- ... <- [  N  ]

Don't think about passing semantics, think in terms of objects in memory and variables containing references to them.

Initial state:

(list)
   |
   V
[  1  ] -> [  2  ] -> ... -> [  N  ]

After list.next = null;:

(list)   (secondElem)
   |          |
   V          V
[  1  ]    [  2  ] -> ... -> [  N  ]

After recursive call:

(list)   (secondElem)      (reverseRest)
   |          |                 |
   V          V                 V
[  1  ]    [  2  ] <- ... <- [  N  ]

Final state after secondElem.Next = list;:

(list)   (secondElem)      (reverseRest)
   |          |                 |
   V          V                 V
[  1  ] <- [  2  ] <- ... <- [  N  ]
忘你却要生生世世 2024-10-06 21:27:22

如果您的目标只是反转列表,请使用 Collections.reverse(list)

If your goal is to simply reverse the list, use Collections.reverse(list)

国粹 2024-10-06 21:27:22

但是,我不明白的是最后几行。

secondElem.next = 列表;
返回反向休息;

让我一一解释一下你不理解的两行:

1.secondElem.next = list;

我认为第二个Elem的创建引入了额外的复杂性。根本不需要变量 secondaryElem。

代替这个:

    // then we reverse everything from the second element on
    ListNode reverseRest = Reverse(secondElem);

    // then we join the two lists
    secondElem.Next = list;

使用这个:

ListNode reverseRest = Reverse(list.next);    
list.next.next = list;

更容易掌握。最后一行代码清楚地显示了列表反转过程本身!

2. return reverseRest;

看看reverseRest变量的用法&了解它的目的。它的唯一目的是将列表的原始尾部的值渗透到所有递归调用的末尾,以便我们可以将原始尾部(也是新的头)返回到调用 Reverse 函数的主函数。您必须快速理解这一点,因为我们根本没有在递归调用之间更改变量reverseRest的值 - 无论我们从 Reverse(secondElem) 收到什么返回值,我们都会从函数中“按原样”返回它!

What I do NOT understand is, however, is the last few lines.

secondElem.next = list;
return reverseRest;

Let me explain you the 2 lines that you did not understand one by one:

1.secondElem.next = list;

I think the creation of secondElem introduces additional complexity. The variable secondElem is not needed at all.

Instead of this:

    // then we reverse everything from the second element on
    ListNode reverseRest = Reverse(secondElem);

    // then we join the two lists
    secondElem.Next = list;

Use this:

ListNode reverseRest = Reverse(list.next);    
list.next.next = list;

It is easier to grasp. The last line of code clearly shows the list reversal process in itself!

2. return reverseRest;

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!

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