ποΈ Deletion Operations in Doubly Linked List (Beginning, End, Specific Position & By Value)
Welcome to Part 3 of our Doubly Linked List in Java series.
In the previous parts, we learned:
β Part 1A
- Introduction to Doubly Linked List
- Node Structure
- Memory Representation
- Head and Tail
β Part 1B
- Creating a Doubly Linked List
- Forward Traversal
- Backward Traversal
β Part 2
- Insertion at Beginning
- Insertion at End
- Insertion at Specific Position
Now we’ll learn another important operation:
ποΈ Deletion
Deletion means removing a node from the linked list.
Unlike a Singly Linked List, a Doubly Linked List allows deletion more easily because every node already knows both its previous and next nodes.
In this tutorial, you’ll learn:
- ποΈ Delete from Beginning
- ποΈ Delete from End
- ποΈ Delete at a Specific Position
- ποΈ Delete by Value
- π» Complete Java Programs
- π Dry Runs
- π Time Complexity
- β Interview Questions
Let’s begin!
π Table of Contents
- What is Deletion?
- Types of Deletion
- Delete from Beginning
- Delete from End
- Delete at Specific Position
- Delete by Value
- Java Programs
- Dry Runs
- Time Complexity
- Common Mistakes
- FAQs
- Conclusion
ποΈ What is Deletion?
Deletion means removing an existing node from the linked list.
Example
Before deletion
NULL β 10 β 20 β 30 β 40 β NULL
Delete
20
Result
NULL β 10 β 30 β 40 β NULL
Notice that the node containing 20 is removed, and the links are updated so that 10 connects directly to 30.
π Types of Deletion
There are four common deletion operations.
1οΈβ£ Delete from Beginning
Before
10 β 20 β 30
After
20 β 30
2οΈβ£ Delete from End
10 β 20 β 30
After
10 β 20
3οΈβ£ Delete at Specific Position
10 β 20 β 30 β 40
Delete Position 3
10 β 20 β 40
4οΈβ£ Delete by Value
10 β 20 β 30 β 40
Delete
30
Result
10 β 20 β 40
π Delete from Beginning
Suppose
Head
β
NULL β 10 β 20 β 30 β NULL
Delete
10
Result
Head
β
NULL β 20 β 30 β NULL
π Algorithm
- Check whether the list is empty.
- Move Head to the second node.
- Set the new Head’s previous reference to NULL.
π¨ Visual Explanation
Current
NULL β 10 β 20 β 30
Update
head = head.next
Update
head.prev = null
Done.
π» Java Program β Delete from Beginning
public void deleteFromBeginning() {
if (head == null) {
System.out.println("List is Empty.");
return;
}
if (head.next == null) {
head = null;
return;
}
head = head.next;
head.prev = null;
}
Output
20 β 30 β NULL
π Time Complexity
O(1)
π Delete from End
Suppose
10 β 20 β 30 β 40
Delete
40
Result
10 β 20 β 30
π Algorithm
- Traverse to the last node.
- Move to the previous node.
- Set its next reference to NULL.
π¨ Visual Explanation
Current
10 β 20 β 30 β 40
Update
30.next = null
Update
40.prev = null
The node containing 40 becomes unreachable.
π» Java Program β Delete from End
public void deleteFromEnd() {
if (head == null) {
System.out.println("List is Empty.");
return;
}
if (head.next == null) {
head = null;
return;
}
Node temp = head;
while (temp.next != null) {
temp = temp.next;
}
temp.prev.next = null;
temp.prev = null;
}
Output
10 β 20 β 30 β NULL
π Time Complexity
Traversal required.
O(n)
π Delete at a Specific Position
Suppose
10 β 20 β 30 β 40 β 50
Delete Position
3
Result
10 β 20 β 40 β 50
π Algorithm
- Traverse to the required node.
- Update the previous node’s next reference.
- Update the next node’s previous reference.
- Disconnect the deleted node.
π¨ Visual Explanation
Current
20 β 30 β 40
Update
20.next = 40
Update
40.prev = 20
Done.
π» Java Program β Delete at Position
public void deleteAtPosition(int position) {
if (head == null) {
System.out.println("List is Empty.");
return;
}
if (position == 1) {
deleteFromBeginning();
return;
}
Node temp = head;
for (int i = 1; i < position && temp != null; i++) {
temp = temp.next;
}
if (temp == null) {
System.out.println("Invalid Position");
return;
}
if (temp.next == null) {
deleteFromEnd();
return;
}
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
temp.prev = null;
temp.next = null;
}
Output
10 β 20 β 40 β 50
π Delete by Value
Suppose
10 β 20 β 30 β 40
Delete
30
Result
10 β 20 β 40
π Algorithm
- Search for the node.
- Update its previous node.
- Update its next node.
- Disconnect the deleted node.
π» Java Program β Delete by Value
public void deleteByValue(int value) {
if (head == null) {
System.out.println("List is Empty.");
return;
}
Node temp = head;
while (temp != null && temp.data != value) {
temp = temp.next;
}
if (temp == null) {
System.out.println("Value Not Found");
return;
}
if (temp == head) {
deleteFromBeginning();
return;
}
if (temp.next == null) {
deleteFromEnd();
return;
}
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
temp.prev = null;
temp.next = null;
}
Output
10 β 20 β 40
π Dry Run
Current
10 β 20 β 30 β 40
Delete
30
Update
20.next = 40
Update
40.prev = 20
Final
10 β 20 β 40
π» Complete Java Program
class Node {
int data;
Node prev;
Node next;
Node(int data) {
this.data = data;
prev = null;
next = null;
}
}
public class DoublyLinkedList {
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;
newNode.prev = temp;
}
public void deleteFromBeginning() {
if (head == null)
return;
if (head.next == null) {
head = null;
return;
}
head = head.next;
head.prev = null;
}
public void deleteFromEnd() {
if (head == null)
return;
if (head.next == null) {
head = null;
return;
}
Node temp = head;
while (temp.next != null)
temp = temp.next;
temp.prev.next = null;
}
public void deleteAtPosition(int position) {
if (position == 1) {
deleteFromBeginning();
return;
}
Node temp = head;
for (int i = 1; i < position && temp != null; i++)
temp = temp.next;
if (temp == null)
return;
if (temp.next == null) {
deleteFromEnd();
return;
}
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
}
public void deleteByValue(int value) {
Node temp = head;
while (temp != null && temp.data != value)
temp = temp.next;
if (temp == null)
return;
if (temp == head) {
deleteFromBeginning();
return;
}
if (temp.next == null) {
deleteFromEnd();
return;
}
temp.prev.next = temp.next;
temp.next.prev = temp.prev;
}
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) {
DoublyLinkedList list = new DoublyLinkedList();
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(40);
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) |
Note: If a Tail pointer is maintained, deletion from the end can be performed in O(1) time.
β οΈ Common Mistakes
β Forgetting to update prev
temp.prev.next = temp.next;
but forgetting
temp.next.prev = temp.prev;
This breaks backward traversal.
β Not checking for an empty list
Always check:
if(head == null)
before accessing node references.
β Forgetting to handle a single-node list
A list containing only one node is a special case and should be handled separately.
β Frequently Asked Questions
Q1. Why is deletion easier in a Doubly Linked List?
Because each node already knows its previous node, making it easy to update links in both directions.
Q2. Why is deletion from the beginning O(1)?
Only the head reference and the new head’s prev reference need to be updated.
Q3. Can deletion from the end be O(1)?
Yes, if the list maintains a Tail pointer.
Q4. Why do we disconnect the deleted node?
Disconnecting the node removes unnecessary references, making it eligible for Java’s Garbage Collector.
Q5. Which references are updated during deletion from the middle?
Typically two main references:
- Previous node’s
next - Next node’s
prev
π― Key Takeaways
- ποΈ Deletion removes a node by updating surrounding references.
- π Deletion from the beginning takes O(1) time.
- πΆ Deletion from the end takes O(n) without a Tail pointer.
- π Both
prevandnextreferences must be updated correctly. - πΎ Deleted nodes are automatically reclaimed by Java’s Garbage Collector when no references remain.
π Conclusion
Deletion in a Doubly Linked List is more convenient than in a Singly Linked List because every node has access to both its previous and next neighbors. This makes updating links simpler and allows efficient bidirectional navigation.
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, complexity analysis, and common pitfalls.
π In Part 4, we’ll explore:
- π Searching in a Doubly Linked List
- βοΈ Updating Node Data
- π’ Counting the Number of Nodes
- π Reversing a Doubly Linked List
- πΆ Forward and Backward Traversal Revisited
- π» Complete Java Programs
- π Time Complexity Analysis
- π― Interview Questions