/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function ReverseList(pHead)
{
// write code here
if(pHead == null || pHead.next == null){
return pHead;
}
var node = pHead;
var p = null;
var q = null;
while(node){
p = node.next;
node.next = q;
q = node;
node = p;
}
return q;
}
这个题如果能自己绘图的话理解最快,这里借用一张牛客网上大佬的图解释一下
翻转链表最重要是就是3+4
三的意思是三个指针:==前 中 后 ==
四的意思是:
while语句中四句代码,我们可以发现这四句正好是头尾相接,记住这个规律可以方便大家的记忆哦。