题解 | #反转链表#
反转链表
http://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
链表反转 双指针法
- 首先,链表的尾结点指向null,先针对两个结点的情况进行分析;
-
- 定义两个指针,分别指向头结点和尾结点null
- 2.遍历当前链表,
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function ReverseList(pHead)
{
// write code here
if(!pHead) return null;
let cur=pHead;
let pre=null;
while(cur){
const temp=cur.next;
cur.next=pre;
pre=cur;
cur=temp;
}
return pre;
// return p2.next;
}
module.exports = {
ReverseList : ReverseList
};