Deletion Operations in Singly Linked List (Beginning, End, Specific Position & By Value)
Welcome to Part 3 of our Singly Linked List in Java series.
In Part 1, we learned:
- ✅ Introduction to Linked Lists
- ✅ Node Structure
- ✅ Creating Nodes
- ✅ Traversing a Linked List
In Part 2, we learned:
- ✅ Insertion at the Beginning
- ✅ Insertion at the End
- ✅ Insertion at a Specific Position
Now it’s time to learn another important operation—Deletion.
Deletion means removing an existing node from the linked list. Since linked lists are connected through references, deleting a node requires updating the links rather than shifting elements, as is done in arrays.
In this tutorial, you’ll learn:
- 🗑️ Deletion from the Beginning
- 🗑️ Deletion from the End
- 🗑️ Deletion at a Specific Position
- 🗑️ Deletion by Value
- 💻 Complete Java Programs
- 🔍 Dry Runs
- 📈 Time Complexity Analysis
- 🎯 Interview Questions
Let’s begin!
📚 Table of Contents
- What is Deletion?
- Why is Deletion Easy in Linked Lists?
- Types of Deletion
- Deletion from the Beginning
- Deletion from the End
- Deletion at a Specific Position
- Deletion by Value
- Java Programs
- Dry Runs
- Time Complexity
- Advantages
- Frequently Asked Questions
- Conclusion
🗑️ What is Deletion?
Deletion means removing a node from a linked list.
For example,
Before deletion
10 → 20 → 30 → 40 → NULL
Delete 20
After deletion
10 → 30 → 40 → NULL
Unlike arrays, no shifting of elements is required.
Only the links between nodes are updated.
🤔 Why is Deletion Easy in Linked Lists?
Consider an array.
10 20 30 40 50
Delete 20
10 30 40 50
All remaining elements shift left.
Time Complexity
[
O(n)
]
Now consider a linked list.
10 → 20 → 30 → 40
Delete 20
Simply make
10 → 30
The node containing 20 becomes disconnected and is eventually removed by Java’s Garbage Collector.
📌 Types of Deletion
There are four common deletion operations.
1️⃣ Delete from Beginning
Before
10 → 20 → 30
After
20 → 30
2️⃣ Delete from End
Before
10 → 20 → 30
After
10 → 20
3️⃣ Delete at a Specific Position
Before
10 → 20 → 30 → 40
Delete Position 3
After
10 → 20 → 40
4️⃣ Delete by Value
Before
10 → 20 → 30 → 40
Delete Value 30
After
10 → 20 → 40
🚀 Deletion from the Beginning
Deleting the first node is the simplest deletion operation.
Suppose we have
Head
↓
10 → 20 → 30 → NULL
To delete 10,
simply move the head to the second node.
Head
↓
20 → 30 → NULL
The first node is no longer referenced.
Java automatically removes it through Garbage Collection.
📝 Algorithm
- Check whether the list is empty.
- If empty, display a message.
- Otherwise, move the head to the next node.
- The old first node becomes unreachable.
💻 Java Program – Delete from Beginning
public void deleteFromBeginning() {
if (head == null) {
System.out.println("Linked List is Empty.");
return;
}
head = head.next;
}
Output
Before
10 -> 20 -> 30 -> NULL
After
20 -> 30 -> NULL
🔍 Dry Run
Current List
Head
↓
10 → 20 → 30
Update
head = head.next;
Result
Head
↓
20 → 30
Done!
📈 Time Complexity
Only one reference is updated.
O(1)
Space Complexity
O(1)
🚀 Deletion from the End
Now consider
10 → 20 → 30 → 40 → NULL
Delete
40
Result
10 → 20 → 30 → NULL
Since this is a Singly Linked List, we cannot move backward.
Therefore, we must reach the second last node.
📝 Algorithm
- Check if the list is empty.
- If only one node exists, set head to NULL.
- Traverse until the second-last node.
- Set its next to NULL.
💻 Java Program – Delete from End
public void deleteFromEnd() {
if (head == null) {
System.out.println("Linked List is Empty.");
return;
}
if (head.next == null) {
head = null;
return;
}
Node temp = head;
while (temp.next.next != null) {
temp = temp.next;
}
temp.next = null;
}
Output
Before
10 -> 20 -> 30 -> 40 -> NULL
After
10 -> 20 -> 30 -> NULL
🔍 Dry Run
Current List
10 → 20 → 30 → 40
Traverse
10
↓
20
↓
30
Stop because
30.next.next == NULL
Update
30.next = NULL
Result
10 → 20 → 30
📈 Time Complexity
Traversal required
O(n)
Space Complexity
O(1)
🚀 Deletion at a Specific Position
Suppose
10 → 20 → 30 → 40 → 50
Delete Position
3
Result
10 → 20 → 40 → 50
📝 Algorithm
- Validate the position.
- If position is 1, delete the first node.
- Traverse to the node before the target.
- Change its next reference.
- The target node becomes disconnected.
💻 Java Program – Delete at a Specific Position
public void deleteAtPosition(int position) {
if (head == null) {
System.out.println("Linked List is Empty.");
return;
}
if (position == 1) {
head = head.next;
return;
}
Node temp = head;
for (int i = 1; i < position - 1 && temp != null; i++) {
temp = temp.next;
}
if (temp == null || temp.next == null) {
System.out.println("Invalid Position");
return;
}
temp.next = temp.next.next;
}
Dry Run
Current List
10 → 20 → 30 → 40 → 50
Delete Position
3
Current node becomes
20
Update
20.next = 40
Result
10 → 20 → 40 → 50
🚀 Deletion by Value
Instead of deleting a position,
delete a specific value.
Suppose
10 → 20 → 30 → 40
Delete
30
Result
10 → 20 → 40
📝 Algorithm
- Check whether the list is empty.
- If the first node contains the value, move the head.
- Otherwise, traverse until the previous node of the target.
- Update its next reference.
💻 Java Program – Delete by Value
public void deleteByValue(int value) {
if (head == null) {
System.out.println("Linked List is Empty.");
return;
}
if (head.data == value) {
head = head.next;
return;
}
Node temp = head;
while (temp.next != null && temp.next.data != value) {
temp = temp.next;
}
if (temp.next == null) {
System.out.println("Value Not Found");
return;
}
temp.next = temp.next.next;
}
Dry Run
Current
10 → 20 → 30 → 40
Find
30
Current node becomes
20
Update
20.next = 40
Result
10 → 20 → 40
💻 Complete Java Program
class Node {
int data;
Node next;
Node(int data) {
this.data = data;
next = null;
}
}
public class SinglyLinkedList {
Node head;
public void insertAtEnd(int data) {
Node newNode = new Node(data);
if (head == null) {
head = newNode;
return;
}
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.next = newNode;
}
public void deleteFromBeginning() {
if (head != null)
head = head.next;
}
public void deleteFromEnd() {
if (head == null)
return;
if (head.next == null) {
head = null;
return;
}
Node temp = head;
while (temp.next.next != null)
temp = temp.next;
temp.next = null;
}
public void deleteAtPosition(int position) {
if (head == null)
return;
if (position == 1) {
head = head.next;
return;
}
Node temp = head;
for (int i = 1; i < position - 1 && temp != null; i++)
temp = temp.next;
if (temp == null || temp.next == null)
return;
temp.next = temp.next.next;
}
public void deleteByValue(int value) {
if (head == null)
return;
if (head.data == value) {
head = head.next;
return;
}
Node temp = head;
while (temp.next != null && temp.next.data != value)
temp = temp.next;
if (temp.next != null)
temp.next = temp.next.next;
}
public void display() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " -> ");
temp = temp.next;
}
System.out.println("NULL");
}
public static void main(String[] args) {
SinglyLinkedList list = new SinglyLinkedList();
list.insertAtEnd(10);
list.insertAtEnd(20);
list.insertAtEnd(30);
list.insertAtEnd(40);
list.insertAtEnd(50);
list.display();
list.deleteFromBeginning();
list.display();
list.deleteFromEnd();
list.display();
list.deleteAtPosition(2);
list.display();
list.deleteByValue(30);
list.display();
}
}
📈 Time Complexity
| Operation | Time Complexity |
|---|---|
| Delete from Beginning | O(1) |
| Delete from End | O(n) |
| Delete at Position | O(n) |
| Delete by Value | O(n) |
💾 Space Complexity
All deletion operations require only a few reference variables.
O(1)
✅ Advantages of Deletion in Linked Lists
- 🚀 No shifting of elements.
- 💾 Efficient memory management.
- 🔗 Only references are updated.
- ⚡ Deletion from the beginning is extremely fast.
❓ Frequently Asked Questions
Q1. Which deletion operation is the fastest?
Deletion from the beginning, because only the head reference changes.
Q2. Why is deleting the last node O(n)?
Because in a singly linked list, we must traverse to the second-last node before updating its next reference.
Q3. What happens to the deleted node in Java?
Since there are no references pointing to it anymore, it becomes eligible for Garbage Collection, and Java automatically reclaims its memory.
Q4. Can we directly delete a node in a singly linked list?
No. We first need access to the previous node so that we can update its next reference.
Q5. What happens if the value or position does not exist?
The operation should be rejected gracefully by displaying an appropriate message or returning without making any changes.
🎯 Key Takeaways
- 🗑️ Deletion removes nodes by updating references.
- 🚀 Deletion from the beginning is O(1).
- 🚶 Deletion from the end, a specific position, or by value requires traversal and takes O(n) time.
- 💾 Java automatically handles memory reclamation using Garbage Collection.
- 🔗 Unlike arrays, linked lists do not require shifting elements after deletion.
🎉 Conclusion
Deletion is one of the major strengths of a Singly Linked List. Instead of shifting data, as in arrays, linked lists simply adjust node references, making deletion efficient and flexible. Understanding how to correctly update these references is essential for mastering linked lists.
In this part, you learned how to delete nodes from the beginning, end, a specific position, and by value, along with complete Java implementations, dry runs, and complexity analysis.
👉 In Part 4, we’ll explore additional linked list operations, including:
- 🔍 Searching for a node
- ✏️ Updating node data
- 🔢 Counting the number of nodes
- 🔄 Reversing a singly linked list
- 🧪 Complete Java programs and interview-focused explanations for each operation.