Cloning a linked list with random pointers is a fascinating yet challenging problem in data structures. This problem frequently appears in coding interviews and competitive programming due to its complexity and practical applications. The task is to duplicate a given linked list where each node contains two pointers: one pointing to the next node in the sequence and the other pointing to a random node within the list (or null). In this blog, we will explore this problem in detail, including approaches, implementation, edge cases, and real-world applications.
What Is a Linked List with Random Pointers?
A linked list with random pointers extends the functionality of a traditional singly linked list. Each node consists of:
- Value (
val
****): The data stored in the node. - Next Pointer (
next
****): Points to the next node in the sequence. - Random Pointer (
random
****): Points to any node in the list, including itself ornull
.

Cloning a Linked List with Random Pointers
Example Structure:
Consider the following linked list:
Value | Next Pointer | Random Pointer |
---|---|---|
1 | Points to 2 | Points to 3 |
2 | Points to 3 | Points to 1 |
3 | Null | Points to 2 |
The task is to create a deep copy of this linked list, ensuring that the new list has the same structure and random pointer references.
Why Is Cloning Such a Linked List Challenging?
Cloning a linked list with only next
pointers is straightforward: you iterate through the list and copy each node’s value while maintaining the sequence. However, the presence of random
pointers adds complexity. Since the random
pointer can reference any node in the list, it’s not possible to directly set up these pointers without extra bookkeeping or traversal.
Cloning a Linked List with Random Pointers
Key challenges include:
- Maintaining the relationships between nodes while creating the deep copy.
- Ensuring that the cloned nodes reference only other cloned nodes for their
random
pointers. - Doing all this efficiently without modifying the original list.
Approaches to Clone a Linked List
There are two main approaches to clone a linked list with random pointers:
1. Naive Approach: Using a Hash Map
In this approach, you use a hash map to store the mapping between the original nodes and their corresponding cloned nodes. This helps in maintaining the random
pointer references.
Steps:
- Traverse the original list and create a deep copy of each node.
- Store the mapping between the original nodes and their clones in a hash map.
- Traverse the original list again to set up the
next
andrandom
pointers for the cloned nodes using the hash map.

Cloning a Linked List
Implementation:
class Node:
def __init__(self, val=0, next=None, random=None):
self.val = val
self.next = next
self.random = random
def clone_with_hash_map(head):
if not head:
return None
# Create a mapping from original nodes to their clones
node_map = {}
current = head
while current:
node_map[current] = Node(current.val)
current = current.next
# Set up the next and random pointers for the cloned nodes
current = head
while current:
if current.next:
node_map[current].next = node_map[current.next]
if current.random:
node_map[current].random = node_map[current.random]
current = current.next
return node_map[head]
Complexity:
- Time Complexity: O(n), where
n
is the number of nodes in the list. - Space Complexity: O(n) for the hash map.
2. Optimized Approach: Without Extra Space
This approach eliminates the need for extra space by weaving the cloned nodes into the original list and then separating them.
Steps:
- Weave the List: Insert each cloned node immediately after its corresponding original node.
- For example, transform
1 → 2 → 3
into1 → 1' → 2 → 2' → 3 → 3'
.
- For example, transform
- Set Random Pointers: For each cloned node, set its
random
pointer to the clone of the original node’srandom
pointer. - Unweave the List: Separate the original and cloned nodes into two distinct lists.
Implementation:
def clone_without_extra_space(head):
if not head:
return None
# Step 1: Weave the cloned nodes into the original list
current = head
while current:
clone = Node(current.val)
clone.next = current.next
current.next = clone
current = clone.next
# Step 2: Set the random pointers for the cloned nodes
current = head
while current:
if current.random:
current.next.random = current.random.next
current = current.next.next
# Step 3: Unweave the list to separate the original and cloned lists
current = head
clone_head = head.next
while current:
clone = current.next
current.next = clone.next
if clone.next:
clone.next = clone.next.next
current = current.next
return clone_head
Complexity:
- Time Complexity: O(n), as we traverse the list three times.
- Space Complexity: O(1), as no additional data structures are used.
Edge Cases to Consider

- Empty List: If the input list is empty, the output should also be empty.
- Single Node: Handle a list with one node, with and without a random pointer.
- All Random Pointers Are Null: Ensure the algorithm handles null random pointers correctly.
- Self-Referencing Nodes: Nodes pointing to themselves via the random pointer.
Applications of Cloning a Linked List
- Data Serialization: Used in serialization and deserialization of complex data structures.
- Graph Representation: Linked lists with random pointers can model adjacency relationships in graphs.
- Advanced Tree Structures: Representing sibling or parent pointers in trees.
- Cross-Referencing Data: Useful in systems requiring cross-references, such as document editors or database systems.
FAQs
Q1: Can this approach handle cycles in the linked list?
Yes, the algorithm can handle cycles in the linked list, as it processes each node independently without relying on linear traversal assumptions.
Q2: How do we verify the cloned list is correct?
Traverse both the original and cloned lists simultaneously to verify that:
- The
val
attributes match. - The
next
pointers form the same sequence. - The
random
pointers reference the correct nodes in the cloned list.
Q3: What if the list has both next
and prev
pointers (doubly linked list)?
The algorithm can be extended to handle doubly linked lists by updating both next
and prev
pointers accordingly.
Conclusion
Cloning a linked list with random pointers is a challenging but rewarding problem that enhances your understanding of pointer manipulation, algorithm optimization, and data structure traversal. By mastering both the naive and optimized approaches, you’ll be well-equipped to tackle similar problems in coding interviews and real-world scenarios. Start implementing these approaches today to sharpen your skills!
Read More –
Detect Loop in a Linked List: A Comprehensive Guide – https://kamleshsingad.com/wp-admin/post.php?post=5483&action=edit
Find the Middle Element of a Linked List: A Complete Guide – https://kamleshsingad.com/wp-admin/post.php?post=5475&action=edit
Cloning a Linked List with Random Pointers – https://kamleshsingad.com/wp-admin/post.php?post=5504&action=edit