杨乐二郑 發表於 2019-9-26 13:51:00

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

<h2 id="toc_0">题目描述</h2>

<p>输入一个链表,反转链表后,输出新链表的表头。</p>

<p>牛客网链接</p>

<h2 id="toc_1">js代码</h2>

<pre><code class="language-javascript">/*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
}
</code></pre><br><br>
来源:https://www.cnblogs.com/dpnlp/p/yongjs-shua-jian-zhioffer-fan-zhuan-lian-biao.html
頁: [1]
查看完整版本: 用js刷剑指offer(反转链表)