用js刷剑指offer(反转链表)

题目描述

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

牛客网链接

js代码

/*function ListNode(x){
    this.val = x;
    this.next = null;
}*/
function ReverseList(pHead)
{
    // write code here
    if (!pHead) return null 
    let p = pHead
    let q = pHead.next
    let head = pHead
    head.next = null
    while (q) {
        p = q
        q = q.next
        p.next = head
        head = p
    }
    return head
}

猜你喜欢

转载自www.cnblogs.com/dpnlp/p/yongjs-shua-jian-zhioffer-fan-zhuan-lian-biao.html