动机出于面试需要,因为之前就看过这些内容,所以其实不怎么费劲
现在再次献上js翻转单链表的代码:(参考<剑指offer>)
(function (){
var node3 ={ //定义有三个节点的链表,这里要注意由与js是顺序解释的,如果开始定义node1,再定义node2,node1.next = node2,其实为undefined,所以链表节点要从尾节点定义
value:"z",
next:null
}
var node2 ={
value:"y",
next:node3
}
var node1 ={
value:"x",
next:node2
}
//从头打印链表各个节点的值,
function printList(listHead){
var p = listHead;
while(p){
console.log(p.value)
p = p.next;
}
}
printList(node1);
function reverse(headList){
var pNode = headList;
var pPrev = null;
var pNext;
while(pNode != null){
pNext = pNode.next;
pNode.next = pPrev; //reverse
pPrev = pNode;
pNode = pNext;
}
return pPrev ; //return newHead
}
printList(reverse(node1));
})();