0
0
DSA Pythonprogramming~20 mins

Clone Linked List with Random Pointer in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Random Pointer Cloning Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of cloning a linked list with random pointers
What is the printed output of the cloned linked list's values and their random pointers after running the given code?
DSA Python
class Node:
    def __init__(self, val, next=None, random=None):
        self.val = val
        self.next = next
        self.random = random

def print_list(head):
    result = []
    current = head
    while current:
        random_val = current.random.val if current.random else None
        result.append(f"{current.val}({random_val})")
        current = current.next
    print(' -> '.join(result) + ' -> None')

def clone_linked_list(head):
    if not head:
        return None

    # Step 1: Insert cloned nodes
    current = head
    while current:
        new_node = Node(current.val, current.next)
        current.next = new_node
        current = new_node.next

    # Step 2: Copy random pointers
    current = head
    while current:
        if current.random:
            current.next.random = current.random.next
        current = current.next.next

    # Step 3: Separate lists
    current = head
    clone_head = head.next
    while current:
        clone = current.next
        current.next = clone.next
        clone.next = clone.next.next if clone.next else None
        current = current.next

    return clone_head

# Create linked list: 1 -> 2 -> 3 -> None
node3 = Node(3)
node2 = Node(2, node3)
node1 = Node(1, node2)

# Setup random pointers
node1.random = node3
node2.random = node1
node3.random = node2

cloned_head = clone_linked_list(node1)
print_list(cloned_head)
A1(3) -> 2(1) -> 3(2) -> None
B1(None) -> 2(None) -> 3(None) -> None
C1(2) -> 2(3) -> 3(1) -> None
D1(1) -> 2(2) -> 3(3) -> None
Attempts:
2 left
💡 Hint
Focus on how random pointers are copied to the cloned nodes.
🧠 Conceptual
intermediate
1:30remaining
Understanding the purpose of interleaving cloned nodes
Why do we insert cloned nodes between original nodes in the linked list during cloning with random pointers?
ATo reverse the linked list in place
BTo sort the linked list nodes by value
CTo remove duplicate nodes from the list
DTo easily assign random pointers of cloned nodes using original nodes' random pointers
Attempts:
2 left
💡 Hint
Think about how the cloned node can find the clone of the random target without extra space.
🔧 Debug
advanced
1:30remaining
Identify the error in this cloning step
What error will this code cause when cloning the random pointers? current = head while current: current.next.random = current.random.next current = current.next.next
ANo error, code runs correctly
BSyntaxError due to missing colon
CAttributeError when current.random is None
DInfinite loop because current is never updated
Attempts:
2 left
💡 Hint
What if a node's random pointer is None?
Predict Output
advanced
2:00remaining
Output after separating cloned list
Given the interleaved list after cloning nodes, what is the output of printing the original list after separation?
DSA Python
class Node:
    def __init__(self, val, next=None, random=None):
        self.val = val
        self.next = next
        self.random = random

def print_list(head):
    result = []
    current = head
    while current:
        random_val = current.random.val if current.random else None
        result.append(f"{current.val}({random_val})")
        current = current.next
    print(' -> '.join(result) + ' -> None')

# Original list: 1 -> 2 -> None
node2 = Node(2)
node1 = Node(1, node2)
node1.random = node2
node2.random = node2

# Interleaved list: 1 -> 1' -> 2 -> 2' -> None
node1_clone = Node(1)
node2_clone = Node(2)
node1.next = node1_clone
node1_clone.next = node2
node2.next = node2_clone
node2_clone.next = None

# Separate lists
current = node1
while current:
    clone = current.next
    current.next = clone.next
    clone.next = clone.next.next if clone.next else None
    current = current.next

print_list(node1)
A1(2) -> 2(2) -> None
B1(None) -> 2(None) -> None
C1(1) -> 2(1) -> None
D1(2) -> 2(None) -> None
Attempts:
2 left
💡 Hint
Check if original nodes' random pointers remain unchanged after separation.
🚀 Application
expert
1:30remaining
Minimum space complexity for cloning linked list with random pointer
What is the minimum extra space complexity to clone a linked list with random pointers without using a hash map or dictionary?
AO(n) - using a hash map to store original to clone mapping
BO(1) - constant extra space by interleaving cloned nodes
CO(log n) - using recursion stack for cloning
DO(n^2) - by nested loops to find random pointers
Attempts:
2 left
💡 Hint
Think about the interleaving technique that avoids extra data structures.