Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new node with the given data.
DSA C++
Node* newNode = new Node([1]);Attempts:
3 left
💡 Hint
Common Mistakes
Using NULL instead of data when creating the node.
Using head or tail instead of data.
✗ Incorrect
We create a new node by passing the data value to the Node constructor.
2fill in blank
mediumComplete the code to check if the list is empty.
DSA C++
if (head == [1]) {
Attempts:
3 left
💡 Hint
Common Mistakes
Checking if head equals newNode or tail.
Checking if head equals head itself.
✗ Incorrect
If head is NULL, the list is empty.
3fill in blank
hardFix the error in linking the new node at the end.
DSA C++
tail->next = [1];Attempts:
3 left
💡 Hint
Common Mistakes
Assigning tail->next to head or tail instead of newNode.
Assigning tail->next to NULL which breaks the list.
✗ Incorrect
The current tail's next should point to the new node.
4fill in blank
hardFill both blanks to update the new node's previous pointer and move the tail.
DSA C++
newNode->prev = [1]; tail = [2];
Attempts:
3 left
💡 Hint
Common Mistakes
Setting newNode->prev to head instead of tail.
Not updating tail to newNode.
✗ Incorrect
The new node's previous points to the old tail, and tail moves to newNode.
5fill in blank
hardFill all three blanks to complete the insert at end function.
DSA C++
void insertAtEnd(Node*& head, Node*& tail, int data) {
Node* newNode = new Node([1]);
if (head == [2]) {
head = tail = newNode;
} else {
tail->next = [3];
newNode->prev = tail;
tail = newNode;
}
}Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names in conditions.
Not linking tail->next to newNode.
✗ Incorrect
Create newNode with data, check if list empty (head == NULL), link tail->next to newNode.
