Complete the code to create a new node and insert it at the beginning of the linked list.
Node* newNode = new Node(data);
newNode->next = [1];
head = newNode;We set the new node's next pointer to the current head, then update head to the new node.
Complete the code to update the head pointer after inserting the new node at the beginning.
head = [1];After linking the new node, head must point to it to mark it as the first node.
Fix the error in the code that inserts a new node at the beginning of the list.
Node* newNode = new Node(data);
newNode->next = [1];
head = newNode;The new node's next must point to the current head, not itself or data.
Fill both blanks to complete the function that inserts a new node at the beginning of the list.
void insertAtBeginning(Node*& head, int data) {
Node* newNode = new Node(data);
newNode->next = [1];
[2] = newNode;
}newNode->next should point to head, and head should be updated to newNode.
Fill all three blanks to complete the code that inserts a new node at the beginning and prints the list.
void insertAtBeginning(Node*& head, int data) {
Node* newNode = new Node(data);
newNode->next = [1];
[2] = newNode;
}
void printList(Node* head) {
Node* current = head;
while (current != [3]) {
std::cout << current->data << " -> ";
current = current->next;
}
std::cout << "null" << std::endl;
}Insert links newNode->next to head and updates head. The print loop ends when current is nullptr.
