Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a pointer to the previous node in a doubly linked list node.
DSA C++
struct Node {
int data;
Node* next;
Node* [1];
};Attempts:
3 left
💡 Hint
Common Mistakes
Using longer or uncommon names like 'previousNode' instead of 'prev'.
Using names that do not clearly indicate the previous node.
✗ Incorrect
The standard name for the pointer to the previous node in a doubly linked list is 'prev'.
2fill in blank
mediumComplete the constructor to initialize the data and set both pointers to nullptr.
DSA C++
struct Node {
int data;
Node* next;
Node* prev;
Node(int val) {
data = val;
next = [1];
prev = [1];
}
};Attempts:
3 left
💡 Hint
Common Mistakes
Using 'NULL' which is legacy and less type-safe.
Using integer 0 which is less clear.
✗ Incorrect
In modern C++, 'nullptr' is used to represent a null pointer safely and clearly.
3fill in blank
hardFix the error in the method that sets the next pointer of the current node.
DSA C++
void setNext(Node* node) {
this->[1] = node;
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning to 'prev' instead of 'next'.
Assigning to 'data' which is an int, not a pointer.
✗ Incorrect
To set the next pointer, assign the parameter 'node' to 'this->next'.
4fill in blank
hardFill both blanks to correctly link two nodes in a doubly linked list.
DSA C++
void linkNodes(Node* first, Node* second) {
first->[1] = second;
second->[2] = first;
}Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up 'next' and 'prev' pointers.
Trying to assign to 'data' which is not a pointer.
✗ Incorrect
The first node's next pointer should point to the second node, and the second node's prev pointer should point back to the first node.
5fill in blank
hardFill all three blanks to create a new node and link it between two existing nodes.
DSA C++
Node* insertBetween(Node* prevNode, Node* nextNode, int val) {
Node* newNode = new Node([1]);
newNode->prev = [2];
newNode->next = [3];
return newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Passing pointers instead of the value to the constructor.
Mixing up prevNode and nextNode assignments.
✗ Incorrect
The new node is created with the value 'val', its prev pointer set to 'prevNode', and its next pointer set to 'nextNode'.
