橘子家的大喵 發表於 2022-7-19 14:36:00

LeetCode 206 Reverse Linked List 反转链表

<pre><code class="language-js">// 递归处理, 利用函数作用域操作当前节点和当前节点的下一个节点
const reverseList = head =&gt; {
    if (head == null || head.next == null) return head
    const end = reverseList(head.next)
    head.next.next = head
    head.next = null
    return end// 返回最后一个节点,也就是反转后的头节点
};

const reverseList = head =&gt; {
    let prev = null
    let cur = head
    while (cur) {
      const next = cur.next
      cur.next = prev
      prev = cur
      cur = next
    }
    return prev
}
</code></pre><br><br>
来源:https://www.cnblogs.com/ltfxy/p/16493970.html
頁: [1]
查看完整版本: LeetCode 206 Reverse Linked List 反转链表