题目
给你一个长度为
n
的链表,每个节点包含一个额外增加的随机指针random
,该指针可以指向链表中的任何节点或空节点。构造这个链表的 深拷贝。 深拷贝应该正好由
n
个 全新 节点组成,其中每个新节点的值都设为其对应的原节点的值。新节点的next
指针和random
指针也都应指向复制链表中的新节点,并使原链表和复制链表中的这些指针能够表示相同的链表状态。复制链表中的指针都不应指向原链表中的节点 。例如,如果原链表中有
X
和Y
两个节点,其中X.random --> Y
。那么在复制链表中对应的两个节点x
和y
,同样有x.random --> y
。返回复制链表的头节点。
用一个由
n
个节点组成的链表来表示输入/输出中的链表。每个节点用一个[val, random_index]
表示:
val
:一个表示Node.val
的整数。random_index
:随机指针指向的节点索引(范围从0
到n-1
);如果不指向任何节点,则为null
。你的代码 只 接受原链表的头节点
head
作为传入参数。
解题思路
- 先排除特殊情况:若链表为null则直接返回null;
- 使用Map存储原始链表的节点和对应索引;
- 使用List来存储复制后链表的节点;
- while循环完成节点复制、next链接和存储;
- 从Map和List中获取首节点进行第二次遍历;
- 第二次遍历建立random链接。
代码展示
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); } }