Point-to-offer

剑指offer第15题:反转链表

题目描述

输入一个链表,反转链表后,输出新链表的表头。

解题

借助三个指针,pre 指向 null,cur 指向 pHead ,temp 记录 cur的下一个结点。

image-20200204161303801

/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function ReverseList(pHead)
{
    var pre = null;
    var cur = pHead;
    
    while(cur !== null){
        var temp = cur.next;
        cur.next = pre;
        pre = cur;
        cur = temp;
    }
    
    return pre;
}

上一篇:14-链表中倒数第k个结点

下一篇:16-合并两个排序的链表