I have the code below to reverse a double linked list using Swift. However, I am confused on whether the swap
function is swapping the currentNode
with the node adjacent to it? or is it swapping it's two adjacent nodes?
我有下面的代码来反转使用Swift的双链表。但是,我对交换函数是否将currentNode与邻近的节点进行交换感到困惑?或者它是否交换它的两个相邻节点?
Example: Linked List values representation: 1 -> 2 -> 3
示例:链接列表值表示:1 - > 2 - > 3
Is it swapping 1 and 2 on the first run? or is it swapping 1 and 3? which values get swapped on the first run?
它是在第一次运行时交换1和2吗?还是交换1和3?哪些值在第一次运行时交换?
public func reverse() {
var node = head
while let currentNode = node {
node = currentNode.next
swap(¤tNode.next, ¤tNode.previous)
head = currentNode
}
}
2 个解决方案
#1
1
You can add a print statement inside the loop (with a newline) to help you debug, or use the debugger to add a breakpoint.
您可以在循环内添加print语句(使用换行符)以帮助您进行调试,或使用调试器添加断点。
We can go through the function together to manually debug as well to improve understanding:
我们可以一起完成这个功能,手动调试以提高理解力:
First run:
第一次运行:
public func reverse() {
var node = head
while let currentNode = node {
node = currentNode.next
swap(¤tNode.next, ¤tNode.previous)
head = currentNode
}
}
In this function:
在这个功能:
- You assign
node
tohead
. They're both pointing to the node with the value1
- 您将节点分配给head。它们都指向值为1的节点
- if
node
exists (it does), then assigncurrentNode
tonode
, socurrentNode = node
, thereforecurrentNode = 1 and node = 1 and head = 1
(all the same node) - 如果节点存在(确实存在),则将currentNode分配给节点,因此currentNode = node,因此currentNode = 1且node = 1且head = 1(所有相同的节点)
- (In the
while
): you say,node = currentNode.next
. So nownode
is2
- (在while中):你说,node = currentNode.next。所以现在节点是2
-
currentNode = 1
still.currentNode.previous
isnil
, andcurrentNode.next
is2
. - currentNode = 1仍然。 currentNode.previous为nil,currentNode.next为2。
- After the swap, the list looks like:
2 -> nil (->) 3
(I put the -> in parens because it doesn't actually "point" to 3 since it's nil). - 在交换之后,列表看起来像:2 - > nil( - >)3(我把 - >放在parens中,因为它实际上并不“指向”3,因为它是nil)。
Note that when I say =
or is, above, like in node = 2
, I mean "node
variable is referring to the Node object with the value of 2"
请注意,当我说=或是,如上所述,就像在node = 2中一样,我的意思是“节点变量指的是值为2的Node对象”
So we're actually swapping the the first node's previous and next.
所以我们实际上交换了第一个节点的上一个节点和下一个节点。
#2
0
The swap call here swaps references to the next and previous element. Because when you're reverting a doubly linked list all "next" pointers should become "previous" and vice versa.
这里的交换调用交换对下一个和前一个元素的引用。因为当你恢复双向链表时,所有“下一个”指针应该变成“前一个”,反之亦然。
Take a look at this image for example
以此图片为例
And imagine that you need to change the marks on the arrows (next should become prev, prev should become next).
并且想象你需要改变箭头上的标记(下一个应该变为上一个,上一个应该成为下一个)。
#1
1
You can add a print statement inside the loop (with a newline) to help you debug, or use the debugger to add a breakpoint.
您可以在循环内添加print语句(使用换行符)以帮助您进行调试,或使用调试器添加断点。
We can go through the function together to manually debug as well to improve understanding:
我们可以一起完成这个功能,手动调试以提高理解力:
First run:
第一次运行:
public func reverse() {
var node = head
while let currentNode = node {
node = currentNode.next
swap(¤tNode.next, ¤tNode.previous)
head = currentNode
}
}
In this function:
在这个功能:
- You assign
node
tohead
. They're both pointing to the node with the value1
- 您将节点分配给head。它们都指向值为1的节点
- if
node
exists (it does), then assigncurrentNode
tonode
, socurrentNode = node
, thereforecurrentNode = 1 and node = 1 and head = 1
(all the same node) - 如果节点存在(确实存在),则将currentNode分配给节点,因此currentNode = node,因此currentNode = 1且node = 1且head = 1(所有相同的节点)
- (In the
while
): you say,node = currentNode.next
. So nownode
is2
- (在while中):你说,node = currentNode.next。所以现在节点是2
-
currentNode = 1
still.currentNode.previous
isnil
, andcurrentNode.next
is2
. - currentNode = 1仍然。 currentNode.previous为nil,currentNode.next为2。
- After the swap, the list looks like:
2 -> nil (->) 3
(I put the -> in parens because it doesn't actually "point" to 3 since it's nil). - 在交换之后,列表看起来像:2 - > nil( - >)3(我把 - >放在parens中,因为它实际上并不“指向”3,因为它是nil)。
Note that when I say =
or is, above, like in node = 2
, I mean "node
variable is referring to the Node object with the value of 2"
请注意,当我说=或是,如上所述,就像在node = 2中一样,我的意思是“节点变量指的是值为2的Node对象”
So we're actually swapping the the first node's previous and next.
所以我们实际上交换了第一个节点的上一个节点和下一个节点。
#2
0
The swap call here swaps references to the next and previous element. Because when you're reverting a doubly linked list all "next" pointers should become "previous" and vice versa.
这里的交换调用交换对下一个和前一个元素的引用。因为当你恢复双向链表时,所有“下一个”指针应该变成“前一个”,反之亦然。
Take a look at this image for example
以此图片为例
And imagine that you need to change the marks on the arrows (next should become prev, prev should become next).
并且想象你需要改变箭头上的标记(下一个应该变为上一个,上一个应该成为下一个)。