Bird
0
0
DSA Cprogramming~10 mins

Clone Linked List with Random Pointer in DSA C - 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 given value.

DSA C
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = [1];
newNode->next = NULL;
newNode->random = NULL;
Drag options to blanks, or click blank then click option'
Avalue
Bdata
Cnode
Dtemp
Attempts:
3 left
💡 Hint
Common Mistakes
Using an undefined variable instead of the input value.
Assigning the pointer instead of the data value.
2fill in blank
medium

Complete the code to insert the cloned node right after the original node.

DSA C
clonedNode->next = original->next;
original->next = [1];
Drag options to blanks, or click blank then click option'
AclonedNode
Boriginal
Ctemp
DnewNode
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning original->next to original instead of clonedNode.
Using an unrelated pointer variable.
3fill in blank
hard

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

DSA C
if (original->random != NULL) {
    original->next->random = original->random[1]2;
}
Drag options to blanks, or click blank then click option'
A->random->
B->next->
C->next
D->random
Attempts:
3 left
💡 Hint
Common Mistakes
Using '->random' instead of '->next' to get the cloned node.
Not checking if original->random is NULL.
4fill in blank
hard

Fill both blanks to separate the cloned list from the original list.

DSA C
original->next = original->next[1];
cloned->next = cloned->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 '->next' only, which does not skip nodes.
Using '->random' which is unrelated here.
5fill in blank
hard

Fill all three blanks to complete the function that clones the linked list with random pointers.

DSA C
struct Node* cloneList(struct Node* head) {
    struct Node* curr = head;
    while (curr != NULL) {
        struct Node* cloned = (struct Node*)malloc(sizeof(struct Node));
        cloned->data = curr->data;
        cloned->next = curr->next;
        curr->next = [1];
        curr = cloned->next;
    }

    curr = head;
    while (curr != NULL) {
        if (curr->random != NULL) {
            curr->next->random = curr->random[2];
        }
        curr = curr->next->next;
    }

    struct Node* original = head;
    struct Node* cloneHead = head ? head->next : NULL;
    struct Node* cloned = cloneHead;
    while (original != NULL && cloned != NULL) {
        original->next = original->next[3];
        cloned->next = cloned->next[3];
        original = original->next;
        cloned = cloned->next;
    }
    return cloneHead;
}
Drag options to blanks, or click blank then click option'
Acloned
B->next
C->next->next
D->random->next
Attempts:
3 left
💡 Hint
Common Mistakes
Not inserting cloned node correctly.
Incorrectly setting random pointers.
Not separating lists properly causing cycles.