Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with given data.
DSA C++
Node* newNode = new Node([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using 'head' instead of data when creating the new node.
Using nullptr which is for pointers, not data.
✗ Incorrect
We create a new node with the given data value to insert at the beginning.
2fill in blank
mediumComplete the code to link the new node's next pointer to the current head.
DSA C++
newNode->next = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode->next to itself causing a cycle.
Setting newNode->next to nullptr which breaks the list.
✗ Incorrect
The new node's next pointer should point to the current head node.
3fill in blank
hardFix the error in updating the previous pointer of the old head node.
DSA C++
if (head != nullptr) head->[1] = newNode;
Attempts:
3 left
💡 Hint
Common Mistakes
Updating next pointer instead of prev.
Trying to assign to data or head which are not pointers.
✗ Incorrect
The previous pointer of the old head should point back to the new node.
4fill in blank
hardFill both blanks to update the head pointer and set the new node's previous pointer.
DSA C++
head = [1]; head->[2] = nullptr;
Attempts:
3 left
💡 Hint
Common Mistakes
Setting head to old head instead of newNode.
Setting head->next to nullptr instead of prev.
✗ Incorrect
The head pointer should point to the new node, and its previous pointer should be nullptr.
5fill in blank
hardFill all three blanks to complete the insert at beginning function.
DSA C++
void insertAtBeginning(Node*& head, int data) {
Node* newNode = new Node([1]);
newNode->next = [2];
if (head != nullptr) head->prev = newNode;
head = newNode;
head->prev = [3];
}Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to update head pointer.
Setting head->prev to newNode instead of nullptr.
✗ Incorrect
Create new node with data, link newNode->next to head, and set head->prev to nullptr.
