Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node for insertion at the end.
DSA C
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = [1];
newNode->next = NULL; Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong variable name for the data field.
Not setting the next pointer to NULL.
✗ Incorrect
We assign the value to the new node's data field before inserting it at the end.
2fill in blank
mediumComplete the code to handle the empty list case when inserting at the end.
DSA C
if (head == NULL) { head = [1]; return head; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning head to NULL instead of the new node.
Using an undefined variable.
✗ Incorrect
If the list is empty, the new node becomes the head of the list.
3fill in blank
hardFix the error in the loop that finds the last node for insertion.
DSA C
struct Node* temp = head; while (temp->[1] != NULL) { temp = temp->next; }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' or 'data' instead of 'next' in the loop condition.
Incorrect loop condition causing infinite loop.
✗ Incorrect
To find the last node, we check the next pointer until it is NULL.
4fill in blank
hardFill both blanks to correctly link the new node at the end.
DSA C
struct Node* temp = head; while (temp->[1] != NULL) { temp = temp->next; } temp->[2] = newNode;
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' or 'data' instead of 'next'.
Not linking the new node properly.
✗ Incorrect
We traverse using 'next' and then link the last node's next to the new node.
5fill in blank
hardFill all three blanks to complete the tail insertion function.
DSA C
struct Node* insertAtEnd(struct Node* head, int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = [1];
newNode->next = NULL;
if (head == NULL) {
head = [2];
return head;
}
struct Node* temp = head;
while (temp->[3] != NULL) {
temp = temp->next;
}
temp->next = newNode;
return head;
} Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names.
Not setting newNode->next to NULL.
Incorrect loop condition.
✗ Incorrect
Assign the value to newNode->data, set head to newNode if empty, and traverse using next pointer.
