3. What will be the output of the following code snippet that creates a singly linked list with nodes 10, 20, 30 and prints the list?
class Node {
int data;
Node next;
Node(int d) { data = d; next = null; }
}
public class LinkedList {
Node head;
public void add(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
}
public void printList() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.print("null");
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add(10);
list.add(20);
list.add(30);
list.printList();
}
}