A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null.
Return a deep copy of the list.
Solution1:Two pass(copy + random) Hashmap
思路:
(1)先处理元素本值复制部分: 遍历原list(nodes_src),对应建立(复制)一个新的list(nodes_copy),节点分别复制;
遍历过程中,建立一个map(node_src -> node_copy),用来后续处理random
(2)random部分的复制: 同步遍历list_src 和 list_copy, node_copy的random为 其(node_copy)对应的node_src的random对应(src->映射map->copy)的node_copy
实现1a: 建立新list的时候直接先连起来,更快些
(实现1b: 不先连,在第二次遍历的时候通过get(node_src->node_copy)来找node_copy再连接,代码看起来简洁,但速度慢)
Time Complexity: O(N) Space Complexity: O(N)
Solution2:依次在旁边复制 连好random 再剥离
思路:Solution1中用hashmap是为了解决random时的node_src->node_copy的对应问题,是因为否则在遍历后我们无法访问到该copy中的random结点。
方法二先将node_copy在原list中复制,-> src -> [copy] -> src -> [copy] -> src -> [copy] -> ...,
这样一来在遍历的过程中,因为node_copy和node_src在同一list中,node_copy(即cur_src.next).random 就是cur_src.random.next(找到random对应的copy结点);
最后再extract the copy list 即可
Time Complexity: O(N) Space Complexity: O(1)
Solution1a Code:
public class Solution {
public RandomListNode copyRandomList(RandomListNode src_head) {
if(src_head == null) return null;
HashMap<RandomListNode, RandomListNode> node_map = new HashMap<>();
// first traversal
// (1)build a new node list by deeply copying from node_src
// (2)build node_map mapping node_src -> node_copy for every node_src
RandomListNode dummy_copy = new RandomListNode(0);
RandomListNode cur_src = src_head;
RandomListNode prev_copy = dummy_copy;
while(cur_src != null) {
RandomListNode node_copy = new RandomListNode(cur_src.label);
prev_copy.next = node_copy;
node_map.put(cur_src, node_copy);
cur_src = cur_src.next;
prev_copy = node_copy;
}
// second traversal
// to add random ptr for each node_copy, by checking the map about which node_copy
// (by corresponding node_src random relationship) that every node_copy.random should points to
cur_src = src_head;
RandomListNode cur_copy = dummy_copy.next;
while(cur_src != null) {
cur_copy.random = node_map.get(cur_src.random);
cur_src = cur_src.next;
cur_copy = cur_copy.next;
}
return dummy_copy.next;
}
}
Solution1b Code:
public class Solution {
public RandomListNode copyRandomList(RandomListNode src_head) {
if(src_head == null) return null;
HashMap<RandomListNode, RandomListNode> node_map = new HashMap<>();
// first traversal
// build node_map mapping node_src -> node_copy for every node_src
RandomListNode cur_src = src_head;
while(cur_src != null) {
RandomListNode node_copy = new RandomListNode(cur_src.label);
node_map.put(cur_src, node_copy);
cur_src = cur_src.next;
}
// second traversal
// to add random ptr for each node_copy, by checking the map about which node_copy; and to link copy_nodes also by getting from map
// (by corresponding node_src random relationship) that every node_copy.random should points to
cur_src = src_head;
while(cur_src != null) {
node_map.get(cur_src).next = node_map.get(cur_src.next);
node_map.get(cur_src).random = node_map.get(cur_src.random);
cur_src = cur_src.next;
}
return node_map.get(src_head);
}
}
Solution2 Code:
public class Solution {
public RandomListNode copyRandomList(RandomListNode src_head) {
if(src_head == null) return null;
// First traversal: Make copy of each node
// -> src -> [copy] -> src -> [copy] -> src -> [copy] -> ...
RandomListNode cur_src = src_head;
RandomListNode cur_src_next_save;
while(cur_src != null) {
cur_src_next_save = cur_src.next;
RandomListNode node_copy = new RandomListNode(cur_src.label);
cur_src.next = node_copy;
node_copy.next = cur_src_next_save;
cur_src = cur_src_next_save;
}
// Second traversal: assign random pointers for the copy nodes
cur_src = src_head;
while(cur_src != null) {
if(cur_src.random != null) {
cur_src.next.random = cur_src.random.next;
}
cur_src = cur_src.next.next;
}
// Third traversal: restore the original list, and extract the copy list.
cur_src = src_head;
RandomListNode dummy_copy = new RandomListNode(0);
RandomListNode cur_copy = dummy_copy;
while(cur_src != null) {
// extract the copy
cur_copy.next = cur_src.next;
cur_copy = cur_copy.next;
// restore the original list
cur_src.next = cur_src.next.next;
cur_src = cur_src.next;
}
return dummy_copy.next;
}
}