📖 Scenario: You are working on a social media app where each user has a friend list. Each friend is represented as a node in a linked list. Besides the usual next friend, each friend node also has a random pointer to any other friend in the list (or null) representing a special connection.To create a backup of this friend list, you need to clone the entire linked list including the random connections.
🎯 Goal: Build a JavaScript program to clone a linked list where each node has a next pointer and a random pointer. The clone should be a deep copy with all next and random pointers correctly assigned.
📋 What You'll Learn
Create a linked list with nodes having val, next, and random properties
Set up a configuration variable to help map original nodes to their clones
Implement the cloning logic to create new nodes and assign next and random pointers
Print the cloned linked list in a readable format showing val and the random node's val or null
💡 Why This Matters
🌍 Real World
Cloning complex linked data structures is useful in social networks, game state saving, and undo-redo systems where objects have multiple references.
💼 Career
Understanding deep copy of linked structures with random pointers is a common interview question and helps in mastering pointer manipulation and hash map usage.
Progress0 / 4 steps
1
Create the original linked list with random pointers
Create a class called Node with properties val, next, and random. Then create three nodes with values 1, 2, and 3. Link them with next pointers: node1.next = node2, node2.next = node3, node3.next = null. Set random pointers as: node1.random = node3, node2.random = node1, node3.random = node2.
DSA Javascript
Hint
Define a Node class first. Then create three nodes and link their next and random pointers exactly as described.
2
Create a map to hold original to cloned node references
Create a new empty Map called nodeMap to store the mapping from original nodes to their cloned nodes.
DSA Javascript
Hint
Use const nodeMap = new Map() to create the map.
3
Clone the linked list nodes and assign next and random pointers
Write code to clone the linked list starting from node1. First, iterate through the original list and create new nodes with the same val, storing them in nodeMap with original nodes as keys. Then iterate again to assign next and random pointers for the cloned nodes using nodeMap.
DSA Javascript
Hint
Use two loops: one to create cloned nodes and store them in nodeMap, another to assign next and random pointers using the map.
4
Print the cloned linked list with random pointers
Write a function called printList that takes a node and prints each node's val and the val of its random pointer (or null) in the format: val (random: random_val) -> .... Then call printList with the cloned head node from nodeMap.get(node1).
DSA Javascript
Hint
Loop through the cloned list and build a string showing each node's value and its random pointer's value or null. Then print the string.
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 values of the nodes only
C. Maintaining the correct random pointer references in the cloned list
D. Sorting the linked list nodes before cloning
Solution
Step 1: Understand the linked list structure
A linked list with random pointers has two references per node: next and random. Cloning must preserve both.
Step 2: Identify the cloning challenge
While copying next pointers is straightforward, random pointers can point anywhere, so they must be carefully mapped to cloned nodes.
Final Answer:
Maintaining the correct random pointer references 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 follow next pointers
Copying only node values without pointers
2. Which of the following JavaScript code snippets correctly creates a new node with value `val` and initializes its `next` and `random` pointers to null?
easy
A. function Node(val) { this.value = val; this.next = null; this.random = null; }
B. function Node(val) { this.val = val; this.next = val; this.random = val; }
C. function Node(val) { this.val = val; this.next = undefined; this.random = undefined; }
D. function Node(val) { this.val = val; this.next = null; this.random = null; }
Solution
Step 1: Check property names and initialization
The node should have properties named val, next, and random. Both next and random should start as null.
Step 2: Verify code correctness
function Node(val) { this.val = val; this.next = null; this.random = null; } correctly assigns val and sets next and random to null. Others either use wrong property names or incorrect initial values.
Final Answer:
function Node(val) { this.val = val; this.next = null; this.random = null; } -> Option D