题解 | #反转链表#

反转链表

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

// 首先的思路是用栈,后来想到是否可以用双向链表,给他添加一个方向再删除原来的

function ListNode(x){
    this.val = x;
    this.next = null;
}

function ReverseList(pHead)
{
    // write code here
//     空链表或只有一个节点的链表
    if (pHead == null || pHead.next == null) {
        return pHead;
    }
    while (pHead.next != null) {
        pHead.next.forward = pHead;
        pHead = pHead.next;
    }
    var point = new ListNode(0);
//     point.next.forward = null;
    point.next = pHead;
    while (point.next.forward != null) {
        point.next.next = point.next.forward;
        point = point.next;
        point.next.next = null;
    }
    


    return pHead
     

}
module.exports = {
    ReverseList : ReverseList
};
全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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