Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to declare a pointer to the next node in a singly linked list node structure.
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 initialization list to set the data value in the node.
DSA C++
struct Node {
int data;
Node* next;
Node(int val) : data([1]), next(nullptr) {}
};Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' instead of 'val' in initialization.
Using 'next' or 'value' which are incorrect here.
✗ Incorrect
The constructor parameter 'val' is used to initialize the 'data' member.
3fill in blank
hardFix the error in the pointer assignment to link two nodes.
DSA C++
Node* first = new Node(10); Node* second = new Node(20); first->[1] = second;
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'prev' which is for doubly linked lists.
Using undefined pointer names like 'link' or 'pointer'.
✗ Incorrect
To link nodes in a singly linked list, assign the 'next' pointer of the first node to the second node.
4fill in blank
hardFill both blanks to complete the function that creates a new node and returns its pointer.
DSA C++
Node* createNode(int val) {
Node* newNode = new Node([1]);
newNode->[2] = nullptr;
return newNode;
}Attempts:
3 left
💡 Hint
Common Mistakes
Using 'data' instead of 'val' in constructor.
Setting 'prev' pointer which does not exist in singly linked list.
✗ Incorrect
The new node is created with 'val' and its 'next' pointer is set to nullptr.
5fill in blank
hardFill all three blanks to complete the function that inserts a new node at the beginning of the list.
DSA C++
void insertAtHead(Node*& head, int val) {
Node* newNode = new Node([1]);
newNode->[2] = head;
head = [3];
}Attempts:
3 left
💡 Hint
Common Mistakes
Assigning head to newNode->next instead of the other way.
Not updating head pointer after insertion.
✗ Incorrect
Create new node with 'val', set its 'next' to current head, then update head to newNode.
