题解 | #反转链表#

反转链表

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

链表反转 双指针法

  • 首先,链表的尾结点指向null,先针对两个结点的情况进行分析;
    1. 定义两个指针,分别指向头结点和尾结点null
  • 2.遍历当前链表,
/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function ReverseList(pHead)
{
    // write code here
    if(!pHead) return null;
    let cur=pHead;
    let pre=null;
    while(cur){
        const temp=cur.next;
        cur.next=pre;
        pre=cur;
        cur=temp;
        
    }
    return pre;
//     return p2.next;

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

相关推荐

昨天 18:49
同济大学 设计
点赞 评论 收藏
分享
nbdy:字太多了,写简历不是写自传,亮点难点技能点列出来就行,要简明扼要
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务