题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function ReverseList(pHead)
{
// write code here
if(pHead == null || pHead.next == null){
return pHead
}
//p1改变后的 头指针 p2尾指针 从左到右改变指针方向
let p1=null,p2=null
while(pHead != null){
p1 = pHead.next
pHead.next = p2
//后移
p2 = pHead
pHead = p1
}
return p2
}
module.exports = {
ReverseList : ReverseList
};