题解 | #反转链表#

反转链表

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
};


#前端##算法学习#
全部评论

相关推荐

点赞 评论 收藏
分享
玉无心❤️:发照片干啥 发简历啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务