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.

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:

1. val: an integer representing Node.val
2. 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.

## Problem solution in Python.

```class Solution(object):
"""
:rtype: Node
"""
return None
check={}

while(cur):
node = Node(cur.val, None, None)
check[cur] = node
cur = cur.next

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

```

## 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;

}

return null;
}

}

}
}
```

## Problem solution in C++.

```class Solution {
public:
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;
}
while(curr != nullptr){
curr->next->random = (curr->random != nullptr ? curr->random->next : nullptr);
curr = curr->next->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) {
int index1 = 0, index2 = 0, index3;
struct Node* copy = malloc(sizeof(struct Node));
struct Node* temp2 = copy;

while(temp1){
temp2 -> val = temp1 -> val;
if (temp1 -> next){
struct Node* temp3 = malloc(sizeof(struct Node));
temp2 -> next = temp3;
temp2 = temp2 -> next;
}
temp1 = temp1 -> next;
}
temp2 -> next = NULL;

temp2 = copy;
while(temp1){
int temp4 = temp1 -> random;
for (int i = 0; i < index1; i++){