What is the output of the following code?
#include <iostream>
using namespace std;
struct Node {
int data;
Node* next;
};
void insertAtEnd(Node** head, int value) {
Node* newNode = new Node();
newNode->data = value;
newNode->next = NULL;
if (*head == NULL) {
*head = newNode;
} else {
Node* current = *head;
while (current->next != NULL) {
current = current->next;
}
current->next = newNode;
}
}
void printList(Node* head) {
Node* current = head;
while (current != NULL) {
cout << current->data << " ";
current = current->next;
}
}
int main() {
Node* head = NULL;
insertAtEnd(&head, 1);
insertAtEnd(&head, 2);
insertAtEnd(&head, 3);
printList(head);
return 0;
}
Correct answer is option 'A'. Can you explain this answer?