题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
这道题做的挺曲折的: 1.设置前后节点的存储 !! 2.当前节点的移动 3.返回的头节点应该是当前节点的前一个节点 !!
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function ReverseList(pHead)
{
let provious = null, after = null;
// 1.改变next属性为之前元素 pHead.next = provious
// 2.结束后改变当前pHead为之后元素,即之前的next属性 pHead = after;
while(pHead) {
after = pHead.next;
pHead.next = provious;
provious = pHead;
pHead = after;
}
// 末尾
return provious;
}
module.exports = {
ReverseList : ReverseList
};