138. 随机链表的复制 --力扣 --JAVA

题目

给你一个长度为 n 的链表,每个节点包含一个额外增加的随机指针 random ,该指针可以指向链表中的任何节点或空节点。

构造这个链表的 深拷贝 。 深拷贝应该正好由 n全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的 next 指针和 random 指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点

例如,如果原链表中有 XY 两个节点,其中 X.random --> Y 。那么在复制链表中对应的两个节点 xy ,同样有 x.random --> y

返回复制链表的头节点。

用一个由 n 个节点组成的链表来表示输入/输出中的链表。每个节点用一个 [val, random_index] 表示:

  • val:一个表示 Node.val 的整数。
  • random_index:随机指针指向的节点索引(范围从 0n-1);如果不指向任何节点,则为 null

你的代码 接受原链表的头节点 head 作为传入参数。

解题思路

  1. 先排除特殊情况:若链表为null则直接返回null;
  2. 使用Map存储原始链表的节点和对应索引;
  3. 使用List来存储复制后链表的节点;
  4. while循环完成节点复制、next链接和存储;
  5. 从Map和List中获取首节点进行第二次遍历;
  6. 第二次遍历建立random链接。

代码展示

java 复制代码
class Solution {
    public Node copyRandomList(Node head) {
        //排除特殊情况
        if (head == null){
            return null;
        }
        Node ans = new Node(head.val);
        //需要获取random的索引,所以用map比遍历List要便捷
        Map<Node, Integer> headMap = new HashMap<>();
        //只需要根据索引获取对应节点,List本身是有序的,所以不需要用Map
        List<Node> ansList = new ArrayList<>();
        int index = 0;
        headMap.put(head, index);
        ansList.add(ans);
        //遍历生成所有节点并存储起来
        while (head.next != null){
            head = head.next;
            ans.next = new Node(head.val);
            index++;
            headMap.put(head, index);
            ansList.add(ans.next);
            ans = ans.next;
        }
        for (Node node : headMap.keySet()){
            if(headMap.get(node) == 0){
                head = node;
                break;
            }
        }
        ans = ansList.get(0);
        while (head != null && ans != null){
            Integer num = headMap.get(head.random);
            Node temp = null;
            if(num != null){
                temp = ansList.get(num);
            }
            ans.random = temp;
            head = head.next;
            ans = ans.next;
        }
        return ansList.get(0);
    }
}
相关推荐
不会计算机的捞地8 分钟前
【数据结构入门训练DAY-30】数的划分
数据结构·算法·深度优先
The_cute_cat1 小时前
试除法判断素数优化【C语言】
算法
Darkwanderor1 小时前
一般枚举题目合集
c++·算法
@我漫长的孤独流浪2 小时前
最短路与拓扑(2)
数据结构·c++·算法
<但凡.3 小时前
C++修炼:多态
开发语言·c++·算法
买了一束花3 小时前
数据预处理之数据平滑处理详解
开发语言·人工智能·算法·matlab
小雅痞3 小时前
[Java][Leetcode simple]26. 删除有序数组中的重复项
java·leetcode
YuforiaCode3 小时前
LeetCode 热题 100 35.搜索插入位置
数据结构·算法·leetcode
Jasmine_llq4 小时前
《P4391 [BalticOI 2009] Radio Transmission 无线传输 题解》
算法·字符串·substr