public class LinkedListPrint {
// Node class representing each element in the linked list
public static class Node {
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
// Prints the entire linked list from head to tail
public static void printList(Node head) {
// Handle empty list
if (head == null) {
System.out.println("Empty list.");
return;
}
// Traverse the list and print each node's data
Node current = head;
while (current != null) {
System.out.print(current.data + " → ");
current = current.next;
}
// Indicate the end of the list
System.out.println("null");
}
public static void main(String[] args) {
// Create linked list: 10 → 20 → 30 → null
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
// Print the linked list
printList(head);
}
}