Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given value.
DSA C++
Node* newNode = new Node([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' instead of 'value' when creating the new node.
Using nullptr which means no value.
✗ Incorrect
We create a new node with the given value to insert at the beginning.
2fill in blank
mediumComplete the code to handle the empty list case by pointing newNode to itself.
DSA C++
if (head == nullptr) { newNode->next = [1]; head = newNode; }
Attempts:
3 left
💡 Hint
Common Mistakes
Setting next to nullptr breaks circular property.
Setting next to head when head is nullptr causes errors.
✗ Incorrect
For an empty circular list, the new node points to itself.
3fill in blank
hardFix the error in the loop that finds the last node before head.
DSA C++
Node* temp = head; while (temp->next != [1]) { temp = temp->next; }
Attempts:
3 left
💡 Hint
Common Mistakes
Using nullptr instead of head causes infinite loop.
Using newNode before it is linked causes errors.
✗ Incorrect
The last node points to head in a circular linked list.
4fill in blank
hardFill both blanks to insert newNode at the beginning and update links.
DSA C++
temp->next = [1]; newNode->next = [2]; head = newNode;
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the order of assignments causes broken links.
Setting newNode->next to temp instead of head.
✗ Incorrect
The last node points to newNode, newNode points to old head, and head updates to newNode.
5fill in blank
hardFill all three blanks to complete the insertAtBeginning function.
DSA C++
void insertAtBeginning(Node*& head, int value) {
Node* newNode = new Node([1]);
if (head == nullptr) {
newNode->next = [2];
head = newNode;
return;
}
Node* temp = head;
while (temp->next != [3]) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = head;
head = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to return after handling empty list.
Using nullptr instead of head in the while loop condition.
✗ Incorrect
Create newNode with value, point newNode->next to itself if empty, and find last node by checking temp->next != head.
