Home >>Java Programs >Singly linked list Examples in Java
A Linked List in Java can be defined as a collection of objects called nodes that are randomly stored in the memory. Each node contains two fields data & address. The last node of the linked list contains the pointer to the null.
public class Main
{
Node head;
static class Node
{
int data;
Node next;
Node(int d)
{
data = d; next=null;
}
}
public void display()
{
Node n = head;
while (n != null)
{
System.out.print(n.data+" \n");
n = n.next;
}
}
public static void main(String[] args)
{
Main list = new Main();
list.head = new Node(100);
Node second = new Node(200);
Node third = new Node(300);
list.head.next = second;
second.next = third;
list.display();
}
}