如何反转链表?
想一想:
Node reverse(Node head) {
Node previous = null;
Node current = head;
Node forward;
while (current != null) {
forward = current.next;
current.next = previous;
previous = current;
current = forward;
}
return previous;
}
到底是如何颠倒列表的呢?
我知道它首先将第二个节点设置为forward
。然后它说 current.next
等于 null
节点 previous
。然后它说先前
现在是当前
。最后当前
变成前进
?
我似乎无法理解这一点以及它是如何逆转的。有人可以解释一下这是如何工作的吗?
Consider:
Node reverse(Node head) {
Node previous = null;
Node current = head;
Node forward;
while (current != null) {
forward = current.next;
current.next = previous;
previous = current;
current = forward;
}
return previous;
}
How exactly is it reversing the list?
I get that it first sets the second node to forward
. Then it says current.next
is equal to a null
node previous
. Then it says previous
is now current
. Lastly current
becomes forward
?
I can't seem to grasp this and how it's reversing. Can someone please explain how this works?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
您迭代地反转列表,并且始终使间隔 [head, previous] 中的列表正确反转(因此当前是其链接未正确设置的第一个节点)。在每个步骤中,您执行以下操作:
如果您对所有节点都这样做,则可以证明(例如使用归纳法)该列表将被正确颠倒。
You reverse the list iteratively and always have the list in the interval [head, previous] correctly reversed (so current is the first node that has its link not set correctly). On each step you do the following:
If you do that for all the nodes, you can prove (with induction for instance) that the list will be correctly reversed.
该代码只是简单地遍历列表并反转链接,直到到达前一个尾部,并将其作为新头返回。
之前:
之后:
The code simply walks the list and inverts the links until it reaches the previous tail, which it returns as the new head.
Before:
After:
最简单的思考方法是这样思考:
图表:
最初:
第一次迭代
第二次迭代
第三次迭代
现在一直循环到最后。所以最后新列表变成:
相同的代码应该是这样的(使其易于理解):
The easiest way to think about it is to think like this:
Diagram:
Initially:
1st Iteration
2nd Iteration
3rd Iteration
Now it keeps looping through till the end. So finally the new list becomes:
The code for the same should be like this (made it easy to understand):
我称之为“樱桃采摘”。这个想法是尽量减少交换次数。交换发生在近索引和远索引之间。这是一个 twp-pass 算法。
示例 1:
示例 2:
I call it "cherry picking". The idea is to minimize the number of swaps. Swapping happens between a near and far index. It's a twp-pass algorithm.
Example 1:
Example 2:
基本思想是将头节点从第一个列表中分离出来,并将其附加到第二个列表的头部。继续重复,直到第一个列表为空。
伪代码:
如果您希望保持原始列表不受干扰,则可以使用辅助函数递归地编写复制版本。
请注意,辅助函数是尾递归的。这意味着您可以使用迭代创建复制反转。
The basic idea is to detach the head node from the first list and attach it to the head of a second list. Keep repeating until the first list is empty.
Pseudocode:
If you wish to leave the original list undisturbed then you can code a copying version recursively with the use of a helper function.
Note that the helper function is tail recursive. This means that you can create a copying reversal using iteration.
使用迭代反转单链表:
Reversing a singly-linked list using iteration:
单链表反转函数的实现:
Implementation of a singly-linked list reversal function:
这是一个简单的函数来反转单链表
}
为了更好地理解,你可以观看这个视频 https://youtu .be/6SYVz-pnVwg
Here is a simple function to reverse a singly linked list
}
For better understanding, you can watch this video https://youtu.be/6SYVz-pnVwg
如果你想使用递归:
If you want to use recursion: