0
0
DSA Pythonprogramming~10 mins

Clone Linked List with Random Pointer in DSA Python - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new node with the same value as the current node.

DSA Python
new_node = Node(current.val[1])
Drag options to blanks, or click blank then click option'
A[]
B()
C{}
D<>
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets [] instead of parentheses.
Using curly braces {} which are for dictionaries or sets.
2fill in blank
medium

Complete the code to link the new node after the current node.

DSA Python
current.next = [1]
Drag options to blanks, or click blank then click option'
Anew_node
Bhead
Ccurrent
Drandom
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning current.next to current itself causes a cycle.
Assigning current.next to head or random is incorrect.
3fill in blank
hard

Fix the error in setting the random pointer of the cloned node.

DSA Python
current.next.random = current.random[1]
Drag options to blanks, or click blank then click option'
A.next
B.next.random
C.next.next
D.random
Attempts:
3 left
💡 Hint
Common Mistakes
Using .random instead of .next leads to wrong pointer.
Using .next.next skips one node too far.
4fill in blank
hard

Fill both blanks to separate the original and cloned lists.

DSA Python
original.next = original.next[1]
clone.next = clone.next[2]
Drag options to blanks, or click blank then click option'
A.next
B.random
C.next.next
D.random.next
Attempts:
3 left
💡 Hint
Common Mistakes
Using .random instead of .next causes wrong links.
Using .random.next is incorrect for next pointers.
5fill in blank
hard

Fill all three blanks to complete the clone function.

DSA Python
def cloneLinkedList(head):
    if not head:
        return None
    current = head
    while current:
        new_node = Node(current.val[1])
        new_node.next = current.next
        current.next = new_node
        current = new_node.next
    current = head
    while current:
        if current.random:
            current.next.random = current.random[2]
        current = current.next.next
    original = head
    clone = head.next
    clone_head = clone
    while original:
        original.next = original.next[3]
        if clone.next:
            clone.next = clone.next.next
        original = original.next
        clone = clone.next
    return clone_head
Drag options to blanks, or click blank then click option'
A()
B.next
D.next.next
Attempts:
3 left
💡 Hint
Common Mistakes
Missing parentheses in constructor call.
Wrong attribute used for random pointer.
Incorrect next pointer update causing cycles.