📖 Scenario: You have a special linked list where each node has two pointers: one points to the next node, and another points randomly to any node in the list or null. This is like a group of friends where each friend points to the next friend in line and also has a secret friend they point to randomly.
🎯 Goal: You will build a program to create a perfect copy of this special linked list. The copy should have new nodes but keep the same next and random connections as the original.
📋 What You'll Learn
Create a Node class with val, next, and random properties
Build the original linked list with exact nodes and connections
Write a function copyRandomList that clones the list with correct next and random pointers
Print the cloned list nodes showing val and the val of their random pointers
💡 Why This Matters
🌍 Real World
Cloning complex linked data structures is useful in applications like undo-redo systems, copying game states, or duplicating complex object graphs.
💼 Career
Understanding how to clone linked lists with random pointers is a common interview question and tests your ability to handle pointers and references carefully.
Progress0 / 4 steps
1
Create the Node class and build the original linked list
Create a class called Node with a constructor that takes val: number and initializes next and random to null. Then create four nodes with values 7, 13, 11, and 10. Link them using next pointers in this order: 7 -> 13 -> 11 -> 10 -> null.
DSA Typescript
Hint
Think of each node as a box with a number and two empty arrows. First, create the boxes, then connect their next arrows in order.
2
Set the random pointers for the original list
Set the random pointers for the nodes as follows: node1.random = null, node2.random = node1, node3.random = node4, and node4.random = node2.
DSA Typescript
Hint
Imagine each friend pointing to their secret friend. Set these secret friend arrows carefully.
3
Write the function to clone the linked list with random pointers
Write a function called copyRandomList that takes the head node head: Node | null and returns a new head node of the cloned list. Use the approach of interleaving cloned nodes between original nodes, then assign random pointers, and finally separate the cloned list from the original.
DSA Typescript
Hint
First, put a copy of each node right after it. Then fix the random arrows for the copies. Finally, pull out the copies to form the new list.
4
Print the cloned list showing values and random pointers
Call copyRandomList with node1 as input and store the result in clonedHead. Then use a while loop with a variable current to traverse clonedHead. For each node, print a line with the format: Node val: X, Random points to: Y where X is the node's val and Y is the val of the random node or null if no random pointer.
DSA Typescript
Hint
Walk through the cloned list and print each node's value and the value of its random pointer or null if none.
Practice
(1/5)
1. What is the main challenge when cloning a linked list with a random pointer?
easy
A. Reversing the linked list before cloning
B. Copying the data values of each node
C. Maintaining the correct random pointer connections in the cloned list
D. Sorting the linked list nodes
Solution
Step 1: Understand the structure of the linked list
The linked list has two pointers: next and random. The random pointer can point to any node or null.
Step 2: Identify the cloning challenge
While copying nodes is straightforward, correctly assigning random pointers in the cloned list is tricky because they may point to nodes not yet cloned.
Final Answer:
Maintaining the correct random pointer connections in the cloned list -> Option C
Quick Check:
Random pointer cloning = main challenge [OK]
Hint: Focus on copying random pointers correctly [OK]
Common Mistakes:
Ignoring random pointers during cloning
Assuming random pointers are always null
Copying only next pointers
2. Which TypeScript syntax correctly declares a node with a random pointer in a linked list?
easy
A. class Node { val: number; next: Node | null; random: Node | null; constructor(val?: number) { this.val = val ?? 0; this.next = null; this.random = null; } }
Cloned values and randoms match original pattern [OK]
Hint: Interleave nodes, assign randoms, then separate [OK]
Common Mistakes:
Not updating random pointers correctly
Mixing original and cloned nodes in output
Confusing next and random values
4. Identify the bug in this TypeScript function that attempts to clone a linked list with random pointers:
function cloneList(head: Node | null): Node | null {
if (!head) return null;
let curr = head;
while (curr) {
const copy = new Node(curr.val);
copy.next = curr.next;
curr.next = copy;
curr = curr.next;
}
curr = head;
while (curr) {
if (curr.random) curr.next!.random = curr.random.next;
curr = curr.next!.next;
}
curr = head;
const pseudoHead = new Node(0);
let copyCurr = pseudoHead;
while (curr) {
copyCurr.next = curr.next;
curr.next = curr.next!.next;
copyCurr = copyCurr.next;
curr = curr.next;
}
return pseudoHead.next;
}
medium
A. Random pointers are not assigned at all
B. In the first while loop, curr is updated incorrectly causing an infinite loop
C. The cloned list is not separated from the original list
D. The function does not handle empty lists
Solution
Step 1: Analyze the first while loop
Inside the loop, curr is updated as curr = curr.next, but curr.next is the cloned node, so curr never moves past cloned nodes, causing infinite loop.
Step 2: Correct the pointer update
It should be curr = copy.next to move to the next original node.
Final Answer:
In the first while loop, curr is updated incorrectly causing an infinite loop -> Option B
Quick Check:
Incorrect curr update = infinite loop [OK]
Hint: Update curr to next original node, not cloned node [OK]
Common Mistakes:
Updating curr to curr.next instead of curr.next.next
Skipping random pointer assignment
Not separating cloned list
5. Given a linked list with random pointers, how would you modify the cloning algorithm to handle the case where some random pointers point to null and others form cycles? Choose the best approach.
hard
A. Ignore random pointers that are null and only clone non-null random pointers
B. Reverse the linked list before cloning to handle cycles
C. Clone nodes and assign random pointers in a single pass without extra space
D. Use a hash map to store original to cloned node mappings and assign random pointers after cloning all nodes
Solution
Step 1: Understand random pointer complexities
Random pointers can be null or form cycles, so direct assignment during cloning is unsafe.
Step 2: Use a hash map for safe cloning
Mapping original nodes to cloned nodes allows assigning random pointers correctly after all nodes are cloned, handling nulls and cycles safely.
Final Answer:
Use a hash map to store original to cloned node mappings and assign random pointers after cloning all nodes -> Option D
Quick Check:
Hash map handles nulls and cycles safely [OK]
Hint: Use hash map to track clones before assigning randoms [OK]
Common Mistakes:
Ignoring null random pointers
Trying to assign randoms in one pass without map
Reversing list unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.