输入一个链表,反转链表后,输出新链表的表头。
示例1
输入:
{1,2,3}
返回值:
{3,2,1}
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function ReverseList(pHead)
{
// write code here
if(!pHead) return
let p = null;
let next = null;
while (pHead) {
next = pHead.next;
pHead.next = p;
p = pHead;
pHead = next;
}
return p
}
module.exports = {
ReverseList : ReverseList
};
本文介绍了一种链表反转的方法,并提供了详细的实现代码。通过迭代方式不断调整节点的指向,最终完成链表的反转。
891

被折叠的 条评论
为什么被折叠?



