题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function ReverseList(pHead)
{
// write code here
let pre = null;
let current = null;
while(pHead){
current = pHead.next;//保存一下当前的指向,也就是下一个的位置
pHead.next = pre;//让当前的phead指向到pre
pre = pHead;//移动原来的pre到下一个节点,原来是null <-- Phead 那就是pHead咯
//此时已完成head结点的摘取及与前一个节点的连接,则我们需要操作下一个结点:故需移动pre和head,让pre指向head,head指向下一个节点。
pHead = current;
}
return pre
}
module.exports = {
ReverseList : ReverseList
};
腾讯公司福利 1143人发布
查看19道真题和解析