每次遇到就地反转的链表就得写好久,几个指针指来指去,搞得晕头转向,这次可把它理清楚了👇👇👇
题目描述
输入一个链表,反转链表后,输出新链表的表头。
思路:
我采用的是就地反转
/*function ListNode(x){
this.val = x;
this.next = null;
}*/
function ReverseList(pHead)
{
// write code here
if(pHead === null || pHead.next === null)
return pHead;
let cur = pHead;
let pNext = null;
let tmp = new ListNode(0);
while(cur.next){
pNext = cur.next;
cur.next = pNext.next;
pNext.next = pHead;
pHead = pNext;
}
return pHead;
}
来源:CSDN
作者:进阶er
链接:https://blog.csdn.net/qq_43466457/article/details/104516525