【每天学习一点算法 2025/12/10】反转链表

每天学习一点算法 2025/12/10

题目:反转链表

给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。

  1. 第一种方法把所有的节点都压入栈中,然后弹栈得到反转后的链表

    typescript 复制代码
    function reverseList(head: ListNode | null): ListNode | null {
      if (head === null || head.next === null) return head
      const stack: ListNode[] = []
      // 遍历链表所有节点入栈
      while (head) {
        stack.push(head)
        head = head.next
      }
      // 获取头节点
      let prev = head = stack.pop() || null
      // 弹栈组成反转后的链表
      while (stack.length > 0) {
        const node = stack.pop()
        prev && (prev.next = node || null)
        prev = node || null
      }
      prev && (prev.next = null)
      return head
    };
  2. 反转操作其实就是把当前节点的 next 指向上一个节点,这样的话我们直接遍历一次就可以实现了

    typescript 复制代码
    function reverseList(head: ListNode | null): ListNode | null {
      if (head === null || head.next === null) return head
      let prev: ListNode | null = null
      let cur: ListNode | null = head
      while (cur) {
        // 取得下一个节点并保存
        const next = cur.next
        // 当前节点的 next 指向上一个节点
        cur.next = prev
        // 移动上一节点指针到当前节点
        prev = cur
        // 移动当前节点指针到下一个节点
        cur = next
      }
      return prev
    }
  3. 官方题解还提供了一种递归的解法,有点复杂这里我也讲一下吧。

    我们假设链表一开始这样的 n(1)→n(2)→n(3)→n(4)→n(5)

    递归的本质就是递到最深层,再归回来处理

    • 首先就是 递到最深层 ,当 head === null || head.next === null 时,就到了递归的最里层,这时的 head 也就是 n(5) 就是反转后链表的 head ,我们通过 newHead 将它返回至最外层作为最终返回值。

      typescript 复制代码
      function reverseList(head: ListNode | null): ListNode | null {
        // 终止条件:空链表 或 遍历到最后一个节点n5
        if (head === null || head.next === null) return head
        // 递:递归遍历下一个节点,直到找到n5(新头节点)
        const newHead = reverseList(head.next)
        // 返回n5作为反转后的head
        return newHead
      }
    • 然后我们就要在 归的过程 中进行反转操作:

      最深层返回 n(5)后,回归到上一层,此时的headn(4) ,我们需要把 n(4)→n(5) 变成 n(5)→n(4)

      因为这里的 newHead 一直都是 n(5),我们只能通过 head.next 来实现反转。

      head.next.next = head ,使 n(5)→n(4)

      head.next = null, 把 n(4)→n(5) 的链接断开避免形成环形。

      然后到上一层head 变成 n(3),把 n(3)→n(4) 转换成 n(4)→n(3),以此类推就这样直到完成整个链表的反转。

      最终代码如下:

      typescript 复制代码
      function reverseList(head: ListNode | null): ListNode | null {
        if (head === null || head.next === null) return head
        const newHead = reverseList(head.next)
        // 归:从n4开始,逐个反转节点指向
        head.next.next = head;
        head.next = null;
      	return newHead
      }

题目来源:力扣(LeetCode)

相关推荐
野犬寒鸦1 分钟前
从零起步学习JVM || 第一章:类加载器与双亲委派机制模型详解
java·jvm·数据库·后端·学习
cpp_25012 分钟前
P10570 [JRKSJ R8] 网球
数据结构·c++·算法·题解
cpp_25019 分钟前
P8377 [PFOI Round1] 暴龙的火锅
数据结构·c++·算法·题解·洛谷
uesowys17 分钟前
Apache Spark算法开发指导-Factorization machines classifier
人工智能·算法
TracyCoder12336 分钟前
LeetCode Hot100(26/100)——24. 两两交换链表中的节点
leetcode·链表
季明洵44 分钟前
C语言实现单链表
c语言·开发语言·数据结构·算法·链表
shandianchengzi1 小时前
【小白向】错位排列|图文解释公考常见题目错位排列的递推式Dn=(n-1)(Dn-2+Dn-1)推导方式
笔记·算法·公考·递推·排列·考公
I_LPL1 小时前
day26 代码随想录算法训练营 回溯专题5
算法·回溯·hot100·求职面试·n皇后·解数独
Yeats_Liao1 小时前
评估体系构建:基于自动化指标与人工打分的双重验证
运维·人工智能·深度学习·算法·机器学习·自动化
only-qi1 小时前
leetcode19. 删除链表的倒数第N个节点
数据结构·链表