A Singly Linked List in Java is one of the simplest data structures used to store a collection of elements. Unlike arrays, a linked list does not keep elements in continuous memory. Instead, each element, called a node, stores two things:
- The value (data)
- A link to the next node
Because each node points to the next one, the entire list forms a chain-like structure.
Example:
10 → 20 → 30 → null
Here, the last node points to null, which means the list ends.
Why do we use a Singly Linked List?
A singly linked list is useful when:
- You want a dynamic structure that can grow and shrink anytime.
- You need fast insertions and deletions, especially at the beginning.
- You want a simple structure to understand how pointers and nodes work.
Arrays are great for fast access, but linked lists are better for flexible memory use.
What you will learn in this chapter
In the code that follows, you will see how to:
- Add elements at the beginning
- Add elements at the end
- Insert at any position
- Delete elements
- Search for a value
- Print the entire list
This simple implementation will help you clearly understand how nodes connect and how the list grows or shrinks.
Full Java Code for Singly Linked List Implementation
// Node class - represents one element in the list
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
// Singly Linked List Implementation
public class SinglyLinkedList {
Node head; // first node
int size; // count of nodes
// Insert at the beginning
public void addFirst(int value) {
Node newNode = new Node(value);
newNode.next = head; // link new node to current head
head = newNode; // update head
size++;
}
// Insert at the end
public void addLast(int value) {
Node newNode = new Node(value);
if (head == null) {
head = newNode; // list was empty
} else {
Node temp = head;
while (temp.next != null) { // go to last node
temp = temp.next;
}
temp.next = newNode; // link last node to new node
}
size++;
}
// Insert at a specific index
public void addAt(int index, int value) {
if (index < 0 || index > size) {
System.out.println("Invalid index");
return;
}
if (index == 0) {
addFirst(value);
return;
}
Node newNode = new Node(value);
Node temp = head;
// move to index - 1
for (int i = 0; i < index - 1; i++) {
temp = temp.next;
}
newNode.next = temp.next; // link
temp.next = newNode; // insert
size++;
}
// Delete first node
public void removeFirst() {
if (head == null) {
System.out.println("List is empty");
return;
}
head = head.next; // move head pointer
size--;
}
// Delete last node
public void removeLast() {
if (head == null) {
System.out.println("List is empty");
return;
}
if (head.next == null) { // only one element
head = null;
} else {
Node temp = head;
while (temp.next.next != null) {
temp = temp.next; // go to second last
}
temp.next = null; // remove last node
}
size--;
}
// Search a value
public int search(int value) {
Node temp = head;
int index = 0;
while (temp != null) {
if (temp.data == value) {
return index; // found
}
temp = temp.next;
index++;
}
return -1; // not found
}
// Print the linked list
public void printList() {
if (head == null) {
System.out.println("List is empty");
return;
}
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " → ");
temp = temp.next;
}
System.out.println("null");
}
// Return size of the list
public int getSize() {
return size;
}
// Test the implementation
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();
// Inserting elements
list.addLast(10);
list.addLast(20);
list.addFirst(5);
list.addAt(2, 15);
System.out.print("List after insertions: ");
list.printList(); // 5 → 10 → 15 → 20 → null
// Deleting elements
list.removeFirst();
list.removeLast();
System.out.print("List after deletions: ");
list.printList(); // 10 → 15 → null
// Searching
int pos = list.search(15);
if (pos != -1) {
System.out.println("15 found at index: " + pos);
} else {
System.out.println("Value not found");
}
// Size
System.out.println("Current size: " + list.getSize());
}
}
This Java code covers:
- Create Node
- Insert at first
- Insert at last
- Insert at any position
- Delete first
- Delete last
- Search for a value
- Print list
- Count nodes (size)
Mini Quiz
Mini Project
Mini Project: Train Coach Management using Singly Linked List
Question:
A train has multiple coaches connected one after another. Create a Java program to manage the train coaches using a Singly Linked List.
Perform the following operations:
- Add a coach at the beginning of the train.
- Add a coach at the end of the train.
- Insert a coach at a specific position.
- Remove the first coach.
- Remove the last coach.
- Search for a coach number.
- Print all the coaches in the train.
- Display the total number of coaches.
View Answer Code
// Node class - represents one coach
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
this.next = null;
}
}
// Train Coach Linked List
public class TrainCoachManagement {
Node head;
int size;
// Add coach at the beginning
public void addFirst(int value) {
Node newNode = new Node(value);
newNode.next = head;
head = newNode;
size++;
}
// Add coach at the end
public void addLast(int value) {
Node newNode = new Node(value);
if (head == null) {
head = newNode;
} else {
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.next = newNode;
}
size++;
}
// Insert coach at a specific position
public void addAt(int index, int value) {
if (index < 0 || index > size) {
System.out.println("Invalid position");
return;
}
if (index == 0) {
addFirst(value);
return;
}
Node newNode = new Node(value);
Node temp = head;
for (int i = 0; i < index - 1; i++) {
temp = temp.next;
}
newNode.next = temp.next;
temp.next = newNode;
size++;
}
// Remove first coach
public void removeFirst() {
if (head == null) {
System.out.println("Train has no coaches.");
return;
}
head = head.next;
size--;
}
// Remove last coach
public void removeLast() {
if (head == null) {
System.out.println("Train has no coaches.");
return;
}
if (head.next == null) {
head = null;
} else {
Node temp = head;
while (temp.next.next != null) {
temp = temp.next;
}
temp.next = null;
}
size--;
}
// Search coach number
public int search(int value) {
Node temp = head;
int index = 0;
while (temp != null) {
if (temp.data == value) {
return index;
}
temp = temp.next;
index++;
}
return -1;
}
// Print coaches
public void printList() {
if (head == null) {
System.out.println("Train has no coaches.");
return;
}
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " → ");
temp = temp.next;
}
System.out.println("null");
}
// Return total coaches
public int getSize() {
return size;
}
public static void main(String[] args) {
TrainCoachManagement train = new TrainCoachManagement();
train.addLast(101);
train.addLast(102);
train.addFirst(100);
train.addAt(2, 150);
System.out.print("Train Coaches: ");
train.printList();
train.removeFirst();
train.removeLast();
System.out.print("After Removing Coaches: ");
train.printList();
int position = train.search(150);
if (position != -1)
System.out.println("Coach 150 found at position: " + position);
else
System.out.println("Coach not found.");
System.out.println("Total Coaches: " + train.getSize());
}
}
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
Shubhranshu Shekhar is a coding instructor, mentor, and founder of VSIT Delhi with 20+ years of teaching experience (since 2004). He has guided many students who are now working in multinational companies and specializes in Full Stack Development, Python, Digital Marketing, and Data Analytics.