前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >剑指offer | 面试题18:反转链表

剑指offer | 面试题18:反转链表

作者头像
千羽
发布2021-12-29 13:15:27
4230
发布2021-12-29 13:15:27
举报
文章被收录于专栏:程序员千羽程序员千羽

死磕算法系列文章

  1. 干货 | 手撕十大经典排序算法
  2. 剑指offer | 认识面试
  3. 剑指offer | 面试题2:实现Singleton模式
  4. 剑指offer | 面试题3:二维数组的查找
  5. 剑指offer | 面试题4:替换空格
  6. 剑指offer | 面试题5:从尾到头打印链表
  7. 剑指offer | 面试题6:重建二叉树
  8. 剑指offer | 面试题7:用两个栈实现队列
  9. 剑指offer | 面试题8:旋转数组的最小数字
  10. 剑指offer | 面试题9:斐波那契数列
  11. 剑指offer | 面试题10:青蛙跳台阶问题
  12. 剑指offer | 面试题11:矩阵覆盖
  13. 剑指offer | 面试题12:二进制中1的个数
  14. 剑指offer | 面试题13:数值的整数次方
  15. 剑指offer | 面试题14:打印从1到最大的n位数

Leetcode : https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof

GitHub : https://github.com/nateshao/leetcode/blob/main/algo-notes/src/main/java/com/nateshao/sword_offer/topic_18_reverseList/Solution.java

剑指 Offer 18. 反转链表

题目描述:定义一个函数,输入一个链表的头节点,反转该链表并输出反转后链表的头节点。

示例:

代码语言:javascript
复制
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL

限制:0 <= 节点个数 <= 5000

解题思路:

如下图所示,题目要求将链表反转。有迭代(双指针)、递归两种实现方法。

方法一:迭代(双指针)

考虑遍历链表,并在访问各节点时修改 next 引用指向,算法流程见注释。

复杂度分析:
  • 时间复杂度 O(N): 遍历链表使用线性大小时间。
  • 空间复杂度 O(1): 变量 precur 使用常数大小额外空间。
代码语言:javascript
复制
package com.nateshao.sword_offer.topic_18_reverseList;

/**
 * @date Created by 邵桐杰 on 2021/11/22 19:41
 * @微信公众号 程序员千羽
 * @个人网站 www.nateshao.cn
 * @博客 https://nateshao.gitee.io
 * @GitHub https://github.com/nateshao
 * @Gitee https://gitee.com/nateshao
 * Description: 反转链表
 * 思路:定义两个指针,反向输出
 * https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof
 */
public class Solution {

    /**
     * 解法一:迭代:两个指针,反向输出,时间复杂度:O(n),空间复杂度:O(1)
     *
     * @param head
     * @return
     */
    public ListNode reverseList(ListNode head) 
        ListNode pre = null;
        ListNode cur = head;
        while (cur != null) {
            ListNode tmp = cur.next;
            cur.next = pre;
            pre = cur;
            cur = tmp;
        }
        return pre;
    }
 /**
     * 解法一:迭代2:两个指针
     * 精选解答
     *
     * @param head
     * @return
     */
    public ListNode reverseList1(ListNode head) {
        ListNode cur = head, pre = null;
        while (cur != null) {
            ListNode tmp = cur.next; // 暂存后继节点 cur.next
            cur.next = pre;          // 修改 next 引用指向
            pre = cur;               // pre 暂存 cur
            cur = tmp;               // cur 访问下一节点
        }
        return pre;
    }
    public class ListNode {
        int val;
        ListNode next;
        ListNode(int x) {
            val = x;
        }
    }
}

方法二:递归

考虑使用递归法遍历链表,当越过尾节点后终止递归,在回溯时修改各节点的 next 引用指向。

recur(cur, pre) 递归函数:
  1. 终止条件:当 cur 为空,则返回尾节点 pre (即反转链表的头节点);
  2. 递归后继节点,记录返回值(即反转链表的头节点)为 res
  3. 修改当前节点 cur 引用指向前驱节点 pre
  4. 返回反转链表的头节点 res

reverseList(head) 函数:

调用并返回 recur(head, null) 。传入 null 是因为反转链表后, head 节点指向 null

复杂度分析:

  • 时间复杂度 O(N)) : 遍历链表使用线性大小时间。
  • 空间复杂度 O(N): 遍历链表的递归深度达到 N ,系统使用 O(N)大小额外空间。
代码语言:javascript
复制
package com.nateshao.sword_offer.topic_18_reverseList;

/**
 * @date Created by 邵桐杰 on 2021/11/22 19:41
 * @微信公众号 程序员千羽
 * @个人网站 www.nateshao.cn
 * @博客 https://nateshao.gitee.io
 * @GitHub https://github.com/nateshao
 * @Gitee https://gitee.com/nateshao
 * Description: 反转链表
 * 思路:定义两个指针,反向输出
 * https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof
 * https://leetcode-cn.com/problems/fan-zhuan-lian-biao-lcof/solution/jian-zhi-offer-24-fan-zhuan-lian-biao-die-dai-di-2/
 */
public class Solution {

    /**
     * 递归:时间复杂度:O(n),空间复杂度:O(n)
     *
     * @param head
     * @return
     */
    public static ListNode reverseList3(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = reverseList3(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }

    public ListNode reverseList4(ListNode head) {
        ListNode cur = head, pre = null;
        while (cur != null) {
            ListNode tmp = cur.next; // 暂存后继节点 cur.next
            cur.next = pre;          // 修改 next 引用指向
            pre = cur;               // pre 暂存 cur
            cur = tmp;               // cur 访问下一节点
        }
        return pre;
    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }

}

革命尚未成功,同志仍需努力,冲冲冲

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-11-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 千羽的编程时光 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 剑指 Offer 18. 反转链表
  • 方法一:迭代(双指针)
  • 方法二:递归
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档