如何反转链表?
2022-09-01 21:40:10
考虑:
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
我似乎无法理解这一点以及它是如何逆转的。有人可以解释一下这是如何运作的吗?