题解 | #反转链表#

反转链表

https://www.nowcoder.com/practice/75e878df47f24fdc9dc3e400ec6058ca

js版本,利用栈的思想,请大佬指教
function ListNode(x){
    this.val = x;
    this.next = null;
}
function ReverseList(pHead)
{
    // write code here
    let s = []
    while(pHead != null){
        s.push(pHead.val)
        pHead = pHead.next
    }
    if(s.length == 0){
        return null
    }
    let node = new ListNode(s.pop())
    let nHead = node
    while(s.length !== 0){
        let temp = new ListNode(s.pop())
        node.next = temp
        node = node.next
    }
    return nHead
}
module.exports = {
    ReverseList : ReverseList
};
全部评论

相关推荐

不愿透露姓名的神秘牛友
07-01 10:56
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-02 17:58
点赞 评论 收藏
分享
05-14 20:34
门头沟学院 Java
窝补药贝八股:管他们,乱说,反正又不去,直接说680
点赞 评论 收藏
分享
Gaynes:查看图片
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务