function reverceList(pHead) {
if (pHead === null || pHead.next === null) {
return pHead
}
// 声明转换变量
let current = pHead,
prev = null,
next = null
// 如果当前节点不为空. 以下规则基本思路是把节点current 与current.next 进行替换 (单链表)
while (current !== null) {
next = current.next // 复制next节点
current.next = prev // 使当前链表的next节点指向临时 prev(是current) 前一个当前节点,进行转换
prev = current // 复制当前节点
current = next // current 与 next 完成替换
}
return prev
}
网友评论