📖 Scenario: Imagine you have a special linked list where each node has two pointers: one points to the next node, and another points to any random node in the list or null. This structure is used in complex applications like undo operations or graph cloning.
🎯 Goal: You will build a program to create a deep copy (clone) of this special linked list. The clone should have new nodes with the same values, next pointers, and random pointers as the original list.
📋 What You'll Learn
Create a Node class with int val, Node next, and Node random fields
Build the original linked list with exact nodes and pointers
Use a helper variable to assist in cloning
Implement the cloning logic to copy nodes and set next and random pointers correctly
Print the cloned list nodes showing val and the val of the random pointer or null
💡 Why This Matters
🌍 Real World
Cloning complex linked data structures is useful in applications like undo-redo systems, graph algorithms, and serialization.
💼 Career
Understanding how to clone linked lists with random pointers is a common interview question and demonstrates mastery of pointers and deep copying.
Progress0 / 4 steps
1
Create the Node class and build the original linked list
Create a Node class with int val, Node next, and Node random fields. Then create three nodes with values 1, 2, and 3. Link them with next pointers: node1 -> node2 -> node3 -> null. Set random pointers as follows: node1.random = node3, node2.random = node1, node3.random = node2.
DSA Java
Hint
Start by defining the Node class with the required fields and constructor. Then create three nodes and link their next and random pointers exactly as described.
2
Add a helper variable to assist cloning
Inside the main method, create a variable called current and set it to node1. This will help you traverse the original list.
DSA Java
Hint
Just create a variable named current and assign it the value node1.
3
Implement the cloning logic for the linked list
Implement the cloning logic inside the main method. First, create new cloned nodes and insert them between original nodes. Then set the random pointers of cloned nodes. Finally, separate the cloned list from the original list. Use variables current, cloneHead, and cloneCurrent as needed.
DSA Java
Hint
Follow the three-step approach: insert cloned nodes, assign random pointers, then separate the cloned list. Use current to traverse and create new nodes.
4
Print the cloned linked list with values and random pointers
Add code to print the cloned linked list starting from cloneHead. For each node, print the format: Node val: X, Random points to: Y where X is the node's value and Y is the value of the node pointed by random or null if random is null.
DSA Java
Hint
Traverse the cloned list starting from cloneHead. For each node, print its val and the val 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. Copying both next and random pointers correctly
B. Only copying the next pointers
C. Ignoring the random pointers
D. Reversing the linked list during cloning
Solution
Step 1: Understand linked list structure
A linked list with random pointers has two references per node: next and random.
Step 2: Identify cloning challenge
Cloning requires copying both next and random pointers so the new list matches the original structure.
Final Answer:
Copying both next and random pointers correctly -> Option A
Quick Check:
Both pointers must be copied = B [OK]
Hint: Remember to copy both next and random pointers [OK]
Common Mistakes:
Copying only next pointers
Ignoring random pointers
Assuming random pointers are null
2. Which Java code snippet correctly declares a node class for a linked list with a random pointer?
easy
A. class Node { int val; Node next; Node random; Node(int val) { val = val; } }
B. class Node { int val; Node next; Node random; Node() { val = 0; } }
C. class Node { int val; Node next; Node random; void Node(int val) { this.val = val; } }
D. class Node { int val; Node next; Node random; Node(int val) { this.val = val; } }
Solution
Step 1: Check constructor syntax
Constructor must have the same name as class and assign value with this.val = val.
Step 2: Validate field declarations
Fields val, next, and random must be declared correctly as int and Node types.
Final Answer:
class Node { int val; Node next; Node random; Node(int val) { this.val = val; } } -> Option D
Quick Check:
Constructor with this.val = val is correct = A [OK]
Hint: Constructor name must match class and use this keyword [OK]
Common Mistakes:
Using void return type for constructor
Not using this keyword for assignment
Incorrect constructor name
3. What will be the output of the following code snippet that clones a linked list with random pointers?
class Node {
int val;
Node next, random;
Node(int val) { this.val = val; }
}
// Original list: 1 -> 2 -> null
// random pointers: 1.random = 2, 2.random = 1
Node head = new Node(1);
head.next = new Node(2);
head.random = head.next;
head.next.random = head;
Node clonedHead = cloneList(head);
System.out.println(clonedHead.val + " -> " + clonedHead.next.val + " -> null");
System.out.println(clonedHead.random.val + " -> " + clonedHead.next.random.val + " -> null");
// Assume cloneList correctly clones the list with random pointers
medium
A. 2 -> 1 -> null
1 -> 2 -> null
B. 1 -> 2 -> null
2 -> 1 -> null
C. 1 -> 2 -> null
1 -> 2 -> null
D. 1 -> 1 -> null
2 -> 2 -> null
Solution
Step 1: Understand original list and random pointers
Original list nodes: 1 -> 2 -> null; random: 1.random = 2, 2.random = 1.
Step 2: Cloned list should replicate values and random links