Copy List with Random Pointer is LeetCode problem 138, commonly asked in Meta (Facebook) interviews with Hash Table and Linked List tags. It requires creating a deep copy of a linked list where each node has a next pointer and a random pointer that can point to any node in the list or null.[1][8]
A linked list of length n is given such that each node contains an additional random pointer random, which could point to any node in the list, or null.
Return the head of a deep copy of the linked list. Each node in the deep copy should have its next pointer set to the next corresponding node in the list, and its random pointer set to the corresponding deep copy node (or null if the original random points to null). The deep copy should not modify the original list.[3][8]
Node Structure (example in typical languages):
class Node { int val; Node next; Node random; Node(int x) { this.val = x; } }
The input/output uses array notation where each [val, random_index] represents a node: val is the node value, and random_index is the 1-indexed position of the random target (or null). Adjacent nodes are connected via next.[8]
Input: head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
Output: [[7,null],[13,0],[11,4],[10,2],[1,0]]
Explanation: Nodes are copied with random pointers preserved (e.g., node 2's random points to node 1 in both original and copy).[3][8]
Input: head = [[1,1],[2,1]]
Output: [[1,1],[2,1]]
Explanation: Node 1's random points to itself; node 2's points to node 1.[8]
Input: head = [[3,null],[3,0],[3,null]]
Output: [[3,null],[3,0],[3,null]].[8]
Node.random is either null or points to some node in the original list.[1][8]