Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare the pointer to the next node in the Node struct.
DSA C++
struct Node {
int data;
Node* [1];
};Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is for doubly linked lists.
Using generic names like 'pointer' which are unclear.
✗ Incorrect
The pointer to the next node in a singly linked list is conventionally named 'next'.
2fill in blank
mediumComplete the constructor to initialize the data and next pointer.
DSA C++
Node(int val) {
data = val;
[1] = nullptr;
}Attempts:
3 left
💡 Hint
Common Mistakes
Initializing 'data' to nullptr which is invalid.
Using 'prev' which does not exist in singly linked list.
✗ Incorrect
The 'next' pointer should be initialized to nullptr to indicate the end of the list.
3fill in blank
hardFix the error in the push_front function to correctly insert a new node at the front.
DSA C++
void push_front(int val) {
Node* newNode = new Node(val);
newNode->[1] = head;
head = newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is not part of singly linked list.
Not linking the new node to the existing list.
✗ Incorrect
The new node's 'next' pointer should point to the current head before updating head.
4fill in blank
hardFill both blanks to complete the function that prints all node data in the list.
DSA C++
void printList() {
Node* current = head;
while (current [1] nullptr) {
std::cout << current->data << " -> ";
current = current->[2];
}
std::cout << "null" << std::endl;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using '==' instead of '!=' in the loop condition.
Using 'prev' pointer which does not exist.
✗ Incorrect
The loop continues while current is not nullptr, and moves to the next node each iteration.
5fill in blank
hardFill all three blanks to complete the function that counts nodes in the list.
DSA C++
int countNodes() {
int count = 0;
Node* current = [1];
while (current [2] nullptr) {
[3];
current = current->next;
}
return count;
}Attempts:
3 left
💡 Hint
Common Mistakes
Starting from nullptr instead of head.
Using '--count' which decreases count.
Using '==' instead of '!=' in the loop condition.
✗ Incorrect
Start from head, loop while current is not nullptr, increment count each iteration.
