Home >>Java Programs >Inserting a new Node at the end of a LinkeList in Java
In this example, we will create a java program of singly linked list and add a new node at the end of the list.
public class Main {
class Node{
int data;
Node next;
public Node(int data) {
this.data = data;
this.next = null;
}
}
public Node head = null;
public Node tail = null;
public void addAtEnd(int data) {
Node newNode = new Node(data);
if(head == null) {
head = newNode;
tail = newNode;
}
else {
tail.next = newNode;
tail = newNode;
}
}
public void display() {
Node current = head;
if(head == null) {
System.out.println("List is empty");
return;
}
System.out.println("Adding nodes to the end of the list: ");
while(current != null) {
System.out.print(current.data + " ");
current = current.next;
}
System.out.println();
}
public static void main(String[] args) {
Main sList = new Main();
sList.addAtEnd(1);
sList.display();
sList.addAtEnd(2);
sList.display();
sList.addAtEnd(3);
sList.display();
sList.addAtEnd(4);
sList.display();
}
}