下面是如何在 JavaScript 中反转链表的方法:
function reverseList(head) {
let prev = null;
let curr = head;
while (curr != null) {
let nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
在这段代码中,我们定义了两个指针 prev
和 curr
,分别指向当前节点的前驱和当前节点。我们还定义了一