考虑:
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
。然后它说previous
是现在current
。最后current
变成forward
?
我似乎无法理解这一点以及它是如何逆转的。有人可以解释一下这是如何工作的吗?