How to Implement Linked List in Java | Insertion & Deletion Explained
Mastering Linked Lists in Java: A Step-by-Step Implementation (Singly Linked List)
In this tutorial, we’ll implement a Singly Linked List in Java. The code includes detailed comments to explain what each part does.
Node Class
// Node represents each element in the Linked List
public class Node {
int data; // Stores the value
Node next; // Points to the next node
// Constructor initializes data and sets next to null
public Node(int data) {
this.data = data;
this.next = null;
}
}
LinkedList Class
// LinkedList class manages all operations
public class LinkedList {
Node head; // The first node in the list
// Constructor initializes the head as null (empty list)
public LinkedList() {
this.head = null;
}
// Check if the list is empty
public boolean isEmpty() {
return this.head == null;
}
// Insert a node at the beginning
public void insertAtBeginning(int data) {
Node newNode = new Node(data); // Create a new node
if (isEmpty()) {
this.head = newNode; // If empty, new node becomes head
} else {
newNode.next = this.head; // Link new node to current head
this.head = newNode; // Update head to new node
}
}
// Insert a node at the end
public void insertAtEnd(int data) {
Node newNode = new Node(data); // Create a new node
if (isEmpty()) {
this.head = newNode; // If list empty, new node is head
} else {
Node temp = this.head; // Start from head
while (temp.next != null) { // Traverse till last node
temp = temp.next;
}
temp.next = newNode; // Attach new node at the end
}
}
// Insert a node after a given value
public void insertAtPosition(int data, int checkedValue) {
Node newNode = new Node(data); // Create a new node
if (isEmpty()) {
this.head = newNode; // If empty, new node is head
} else {
Node temp = this.head;
while (temp != null) {
if (temp.data == checkedValue) { // Found the target value
newNode.next = temp.next; // Link new node to next
temp.next = newNode; // Insert after target node
break;
}
temp = temp.next; // Move to next node
}
}
}
// Print the Linked List
public void printList() {
if (isEmpty()) {
System.out.println("Linked List is Empty");
} else {
Node temp = this.head;
while (temp != null) { // Traverse all nodes
System.out.print(temp.data + " "); // Print data
temp = temp.next; // Move to next node
}
System.out.println();
}
}
// Delete the first node
public void deleteFromBeginning() {
if (!isEmpty()) {
this.head = this.head.next; // Head moves to next node
}
}
// Delete the last node
public void deleteFromEnd() {
if (!isEmpty()) {
if (this.head.next == null) {
this.head = null; // Only one node -> make list empty
} else {
Node temp = this.head;
while (temp.next.next != null) { // Find 2nd last node
temp = temp.next;
}
temp.next = null; // Remove link to last node
}
}
}
// Delete a specific node by value
public void deleteFromSpecificPosition(int deleted) {
if (!isEmpty()) {
if (this.head.data == deleted) { // If head node matches
this.head = this.head.next;
return;
}
Node temp = this.head;
Node prev = null;
while (temp != null) {
if (temp.data == deleted) { // Found node to delete
prev.next = temp.next; // Bypass the node
return;
}
prev = temp; // Store previous node
temp = temp.next; // Move forward
}
}
}
}
Main Class – Testing the Linked List
// Main class to test LinkedList operations
public class Main {
public static void main(String[] args) {
LinkedList myList = new LinkedList();
// Insert nodes at beginning and end
myList.insertAtBeginning(10);
myList.insertAtEnd(20);
myList.insertAtBeginning(30);
myList.insertAtEnd(40);
myList.printList(); // Output: 30 10 20 40
// Insert one more node at beginning
myList.insertAtBeginning(5);
myList.printList(); // Output: 5 30 10 20 40
// Insert 35 after node with value 30
myList.insertAtPosition(35, 30);
myList.printList(); // Output: 5 30 35 10 20 40
// Delete first node
myList.deleteFromBeginning();
myList.printList(); // Output: 30 35 10 20 40
// Delete last node
myList.deleteFromEnd();
myList.printList(); // Output: 30 35 10 20
// Delete node with value 20
myList.deleteFromSpecificPosition(20);
myList.printList(); // Output: 30 35 10
}
}
Final Output
30 10 20 40
5 30 10 20 40
5 30 35 10 20 40
30 35 10 20 40
30 35 10 20
30 35 10
Conclusion
With comments explaining each step, it’s easier to understand how a Singly Linked List works internally. By practicing insertions, deletions, and traversals, you’ll strengthen your foundation in Data Structures, which is essential for advanced topics like stacks, queues, and graphs.

Comments
Post a Comment