我有下面的代码来使用Swift颠倒双链表。不过,我很困惑swap
函数是否将currentNode
与相邻的节点交换?还是交换它的两个相邻节点?使用Swift颠倒双链表
示例: 链表值表示: 1 - > 2 - > 3
是它在第一次运行交换1和2?还是交换1和3?哪些值在第一次运行时被交换?
public func reverse() {
var node = head
while let currentNode = node {
node = currentNode.next
swap(¤tNode.next, ¤tNode.previous)
head = currentNode
}
}
但在第一次运行时,不是A.prev = null? –
@ Maria9905它是。当你交换值时,你会得到A.next = null - 因为你的第一个元素成为你的最后一个元素。 – algrid