In this **Leetcode Copy List with Random Pointer problem solution** A linked list of length n is given such that each node contains an additional random pointer, which could point to any node in the list or null.

Construct a deep copy of the list. The deep copy should consist of exactly n brand new nodes, where each new node has its value set to the value of its corresponding original node. Both the next and random pointer of the new nodes should point to new nodes in the copied list such that the pointers in the original list and copied list represent the same list state. None of the pointers in the new list should point to nodes in the original list.

For example, if there are two nodes X and Y in the original list, where X.random --> Y, then for the corresponding two nodes x and y in the copied list, x.random --> y.

Return the head of the copied linked list.

The linked list is represented in the input/output as a list of n nodes. Each node is represented as a pair of [val, random_index] where:

- val: an integer representing Node.val
- random_index: the index of the node (range from 0 to n-1) that the random pointer points to, or null if it does not point to any node.

Your code will only be given to the head of the original linked list.

## Problem solution in Python.

class Solution(object): def copyRandomList(self, head): """ :type head: Node :rtype: Node """ if not head: return None check={} cur = head while(cur): node = Node(cur.val, None, None) check[cur] = node cur = cur.next cur = head while(cur): if cur.next is not None: check[cur].next = check[cur.next] if cur.random is not None: check[cur].random = check[cur.random] cur = cur.next return check[head]

## Problem solution in Java.

class Solution { Map<Node, Node> mappings = new HashMap<>(); public Node getClone(Node oldNode) { if(oldNode != null) { if(!mappings.containsKey(oldNode)) { Node newNode = new Node(oldNode.val); this.mappings.put(oldNode, newNode); } return this.mappings.get(oldNode); } return null; } public Node copyRandomList(Node head) { if(head == null){ return null; } Node oldHead = head; Node newHead = new Node(oldHead.val); mappings.put(oldHead, newHead); while(oldHead != null) { newHead = mappings.get(oldHead); newHead.next = this.getClone(oldHead.next); newHead.random = this.getClone(oldHead.random); newHead = newHead.next; oldHead = oldHead.next; } return this.mappings.get(head); } }

## Problem solution in C++.

class Solution { public: Node* copyRandomList(Node* head) { if (head == nullptr) return nullptr; Node* next, *temp, *curr; for(curr = head ; curr != nullptr ;){ next = curr->next; curr->next = new Node(curr->val); curr->next->next = next; curr = next; } curr = head; while(curr != nullptr){ curr->next->random = (curr->random != nullptr ? curr->random->next : nullptr); curr = curr->next->next; } Node* original = head, *copy = head->next; temp = copy; while(original && copy){ original->next = (original->next != nullptr ? original->next->next : nullptr); copy->next = (copy->next != nullptr ? copy->next->next : nullptr); original = original->next; copy = copy->next; } return temp; } };

## Problem solution in C.

struct Node* copyRandomList(struct Node* head) { if (!head) {return NULL;} int index1 = 0, index2 = 0, index3; int* address1 = malloc(sizeof(int)*1024); int* address2 = malloc(sizeof(int)*1024); struct Node* temp1 = head; struct Node* copy = malloc(sizeof(struct Node)); struct Node* temp2 = copy; while(temp1){ temp2 -> val = temp1 -> val; address1[index1++] = temp1; address2[index2++] = temp2; if (temp1 -> next){ struct Node* temp3 = malloc(sizeof(struct Node)); temp2 -> next = temp3; temp2 = temp2 -> next; } temp1 = temp1 -> next; } address1[index1++] = NULL; address2[index2++] = NULL; temp2 -> next = NULL; temp1 = head; temp2 = copy; while(temp1){ int temp4 = temp1 -> random; for (int i = 0; i < index1; i++){ if (temp4 == address1[i]){ index3 = i; break; } } temp2 -> random = (struct Node*) address2[index3]; temp1 = temp1 -> next; temp2 = temp2 -> next; } return copy; }

## 0 Comments