题解 | #反转链表#

反转链表

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
};
全部评论

相关推荐

认真搞学习:28小登的建议,投算法岗不要写什么物理竞赛,互联网+,多写点项目,用什么算法做了什么。还有本科算法是不可能的开发你这个也没有项目啊
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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