Complete the code to declare an array of 5 integers in C++.
int arr[[1]];The array size must be specified as 5 to create an array of 5 integers.
Complete the code to create a singly linked list node struct in C++.
struct Node {
int data;
[1] *next;
};The next pointer must be of type Node* to point to the next node in the linked list.
Fix the error in the code to insert a new node at the beginning of a linked list.
void insertAtHead(Node*& head, int val) {
Node* newNode = new Node();
newNode->data = val;
newNode->next = [1];
head = newNode;
}The new node's next pointer should point to the current head before updating head to newNode.
Fill both blanks to create a function that returns the length of a linked list.
int length(Node* head) {
int count = 0;
while (head [1] nullptr) {
count++;
head = head[2];
}
return count;
}The loop continues while head is not nullptr, and head moves to the next node using head->next.
Fill all three blanks to create a function that converts an array to a linked list.
Node* arrayToList(int arr[], int size) {
Node* head = nullptr;
for (int i = [1]; i [2] size; i++) {
Node* newNode = new Node();
newNode->data = arr[i];
newNode->next = head;
head = [3];
}
return head;
}The loop starts at 0, runs while i is less than size, and head is updated to newNode each iteration.
