// Define the Node classpublicclassNode {
publicint data;
public Node next;
publicNode(int data) {
this.data = data;
this.next = null;
}
}
// Define the LinkedList classpublicclassLinkedList {
public Node head;
publicLinkedList() {
this.head = null;
}
// Method to insert new node at end of the linked listpublicvoidInsert(int data) {
Node newNode = new Node(data);
// If list is empty, new node becomes the headif (this.head == null) {
this.head = newNode;
return;
}
Node current = this.head;
// Traverse the list until end is reachedwhile (current.next != null) {
current = current.next;
}
// Insert the new node at the end of the list current.next = newNode;
}
}