Skip to content

Practice Copy List with Random Pointer

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Medium#138
LeetCode Problem

Copy List with Random Pointer

Problem Statement

Create a deep copy of a linked list whose nodes have next and random pointers. Each random pointer is null or targets any node in the original list. Allocate a corresponding new node for every original node, preserving values and pointer structure; no copied pointer may target an original node. Return the copied head. The serialized representation uses [val, random_index] for each node: random_index is the zero-based target position or null. The function receives the original head, not those serialization pairs.

Source: Copy List with Random Pointer on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
head = [[7,null],[13,0],[11,4],[10,2],[1,0]]
Output
[[7,null],[13,0],[11,4],[10,2],[1,0]]

# Example 2

Input
head = [[1,1],[2,1]]
Output
[[1,1],[2,1]]

# Example 3

Input
head = [[3,null],[3,0],[3,null]]
Output
[[3,null],[3,0],[3,null]]

Constraints

0 <= n <= 1000
-10^4 <= Node.val <= 10^4
Node.random is null or is pointing to some node in the linked list.

Related LeetCode Problems

Practice more problems similar to Copy List with Random Pointer.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.