题解 | #反转链表#
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
js版本,利用栈的思想,请大佬指教
function ListNode(x){
this.val = x;this.next = null;
}
function ReverseList(pHead)
{
// write code here
let s = []
while(pHead != null){
s.push(pHead.val)
pHead = pHead.next
}
if(s.length == 0){
return null
}
let node = new ListNode(s.pop())
let nHead = node
while(s.length !== 0){
let temp = new ListNode(s.pop())
node.next = temp
node = node.next
}
return nHead
}
module.exports = {
ReverseList : ReverseList
};