Problem
You get head, the first node of a linked list with n nodes. Each node has a value val, a link next to the next node and a second link random. The random link points to any node in the list, or to no node.
Make a deep copy of the list and return the head of the copy. A deep copy has n new nodes with the same values. The next and random links of the new nodes have the same pattern as in the original list.
No link in the copy can point to a node of the original list. Do not change the original list. Make a new node with Node(val).
The input and the output show each node as a pair [val, random_index]. random_index is the index of the node that random points to, or null. Your function gets the head node, not this list.
Example 1
- Input:
- head = [[5,null],[9,0],[2,3],[6,1]]
- Output:
- [[5,null],[9,0],[2,3],[6,1]]
- Explanation:
- The
randomlink of the node with the value 9 points to the node at index 0. That node has the value 5.
Example 2
- Input:
- head = [[4,1],[8,1]]
- Output:
- [[4,1],[8,1]]
- Explanation:
- The
randomlink of the second node points to the second node itself.
Example 3
- Input:
- head = []
- Output:
- []
- Explanation:
- The list is empty, so the copy is also empty.
Constraints
- 0 <= n <= 1000
- -10^4 <= Node.val <= 10^4
- Node.random is null or points to a node in the linked list.
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
- Linked List Cycle
Coding · Floyd's Cycle Finding Algorithm · Hash Table · Linked List
- All O`one Data Structure
Coding · Design · Doubly-Linked List · Hash Table
- Design Authentication Manager
Coding · Design · Doubly-Linked List · Hash Table
- Design HashMap
Coding · Array · Design · Hash Function
Sources · 2 references
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 3 records
Amazon
Leetcode 138. Copy List with Random Pointer
Reported Feb 8, 2026 · 1 source report
SENIOR
Source snapshot: Nov 12, 2026
View sourceMeta
Leetcode 138. Copy List with Random Pointer
Reported Aug 1, 2026 · 16 source reports
MID_LEVEL, SENIOR, STAFF
Source snapshot: Nov 12, 2026
View sourceMicrosoft
Leetcode 138. Copy List with Random Pointer
Reported Jan 15, 2026 · 1 source report
MID_LEVEL
Source snapshot: Nov 12, 2026
View source
LeetCode company tags · 1 record
NVIDIA
Copy List with Random Pointer