题解 | #反转链表#
反转链表
https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca
#test01 反转链表1--2--3--null ==> 3--2--1--null
两年没看算法了,看了各位大佬的题解!!!
/*function ListNode(x){ this.val = x; this.next = null; }*/ function ReverseList(pHead) { // write code here // 判断链表为空或长度为1的情况 if(pHead == null || pHead.next == null){ return pHead; } let p1= null, p2 = null; while(pHead) { p1 = pHead.next pHead.next = p2 p2 = pHead pHead = p1 } return p2; } module.exports = { ReverseList : ReverseList };