剑指 Offer 35. 复杂链表的复制

这篇博客讨论了如何复制一个包含随机指针的复杂链表。提供了两种不同的解决方案,一种是使用HashMap建立节点映射关系并构建新链表,另一种是通过拼接和拆分原链表来构造新链表。这两种方法的时间复杂度均为O(n),空间复杂度分别为O(n)和O(1)。
摘要由CSDN通过智能技术生成

剑指 Offer 35. 复杂链表的复制

题目

请实现 copyRandomList 函数,复制一个复杂链表。在复杂链表中,每个节点除了有一个 next 指针指向下一个节点,还有一个 random 指针指向链表中的任意节点或者 null。

 

示例 1:



输入:head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
输出:[[7,null],[13,0],[11,4],[10,2],[1,0]]
示例 2:



输入:head = [[1,1],[2,1]]
输出:[[1,1],[2,1]]
示例 3:



输入:head = [[3,null],[3,0],[3,null]]
输出:[[3,null],[3,0],[3,null]]
示例 4:

输入:head = []
输出:[]
解释:给定的链表为空(空指针),因此返回 null。
 

提示:

-10000 <= Node.val <= 10000
Node.random 为空(null)或指向链表中的节点。
节点数目不超过 1000 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/fu-za-lian-biao-de-fu-zhi-lcof
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

前置知识:

标签:

思路

1.自己的思路

使用hashMap存储映射关系,并构建新的复杂链表

代码

class Offer35V0{
    public Node copyRandomList(Node head) {
        Node cur = head;
        HashMap<Node, Node> map = new HashMap<>();

        //复制原始链表的节点,并建立映射关系
        while (cur != null){
            map.put(cur, new Node(cur.val));
            cur = cur.next;
        }

        cur = head;
        while (cur != null){
            //map.get(cur) 表示原链表节点映射的新链表的节点
            map.get(cur).next = map.get(cur.next);
            map.get(cur).random = map.get(cur.random);
            cur = cur.next;
        }
        return map.get(head);
    }
}

复杂度分析

  • 时间复杂度:O(n)
  • 空间复杂度:O(n)

2.题解思路(拼接+拆分)

img

  • 复制各节点,构建拼接链表:

    • 设原链表为 n o d e 1 → n o d e 2 → ⋯ node1→node2→⋯ node1node2 ,构建的拼接链表如下所示:
      n o d e 1 → n o d e 1 n e w → n o d e 2 → n o d e 2 n e w → ⋯ node1 \rightarrow node1_{new} \rightarrow node2 \rightarrow node2_{new} \rightarrow ⋯ node1node1newnode2node2new
  • 构建新链表各节点的 random 指向:

    • 当访问原节点 cur的随机指向节点cur.random 时,对应新节点 cur.next的随机指向节点为 cur.random.next
  • 拆分原 / 新链表:

    • 设置 pre / cur分别指向原 / 新链表头节点,遍历执行 pre.next = pre.next.nextcur.next = cur.next.next 将两链表拆分开。
  • 返回新链表的头节点 res 即可。

代码

class Offer35V1{
    public Node copyRandomList(Node head) {
       Node cur = head;
       //复制各节点,并构建拼接链表
        while (cur != null){
            Node temp = new Node(cur.val);
            temp.next = cur.next;
            cur.next = temp;
            cur = temp.next;
        }

        //构建各新节点的 random 指向
        cur = head;
        while (cur != null){
            if (cur.random != null){
                cur.next.random = cur.random.next;
            }
            cur = cur.next;
        }

        //拆分
        cur = head.next;
        Node pre = head, res = head.next;
        while (cur.next != null){
            pre.next = pre.next.next;
            cur.next = cur.next.next;
            pre = pre.next;
            res = res.next;
        }
        // 单独处理原链表尾节点
        res.next = null;
        // 返回新链表头节点
        return res;
    }
}

复杂度分析

  • 时间复杂度:O(n),三轮遍历链表,使用 O(n) 时间。
  • 空间复杂度:O(1), 节点引用变量使用常数大小的额外空间

总结

链表类题目还是需要画图

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

clock_town

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值