Deletion from the beginning in a Linked List in Java
Java Program for Deletion from the beginning in a Linked List
In this article you will learn how Deletion from the Beginning in a Linked List in Java works, how to perform it step by step, and how to implement it using different approaches. This operation is very important for understanding how linked list pointers work and is commonly asked in coding interviews.
Deletion from the beginning in a linked list is one of the simplest and most frequently used operations in data structures.
Deletion from the beginning in a Linked List in Java
What is Deletion from the Beginning in a Linked List?
In a singly linked list, each node holds data and a pointer to the next node.
Deletion from the beginning simply means removing the first node (head node) and making the next node the new head.
Example:
Before deletion: 10 → 20 → 30 → 40
After deletion: 20 → 30 → 40
Only the head pointer changes. No other nodes move.
Learning Deletion from the Beginning in a Linked List in Java helps you understand:
- Node removal
- Pointer updates
- Memory release (Java Garbage Collector handles it)
- Handling edge cases (empty list, single node list)
1. Pointing the head to the next node
2. Disconnecting the old head
3. Returning the new head
This operation always runs in constant time because it manipulates only one pointer.
Methods for Deletion from the beginning in a Linked List in Java
Here on this page we will discuss 2 different methods for Deletion from the beginning in a Linked List in Java:
Learn DSA
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Methods for Deletion from the beginning in a Linked List in Java
Method 1: Simple Pointer Update (Iterative)
Algorithm:
- Let head be the starting node of the list.
- If head is null, return null (list is empty).
- Store head.next in a temporary pointer newHead.
- Disconnect the old head by setting head.next = null.
- Return newHead as the updated head.
Java Code:
class DeleteBegin {
static class Node {
int data;
Node next;
Node(int data) { this.data = data; }
}
public static Node deleteFromBeginning(Node head) {
if (head == null) return null;
Node newHead = head.next;
head.next = null; // disconnect old head
return newHead;
}
public static void printList(Node head) {
while (head != null) {
System.out.print(head.data + " ");
head = head.next;
}
System.out.println();
}
public static void main(String[] args) {
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
System.out.println("Before deletion:");
printList(head);
head = deleteFromBeginning(head);
System.out.println("After deletion:");
printList(head);
}
}
Input:
10 20 30
Output:
20 30
Space Complexity: O(1)
Methods for Deletion from the beginning in a Linked List
Method 2: Deletion Using Linked List Class Wrapper
Algorithm:
Create a LinkedList class with a head pointer.
If head is null, do nothing.
Assign head = head.next.
Old head is automatically removed by garbage collection.
Java Code:
class LinkedListDeletion {
static class Node {
int data;
Node next;
Node(int data) { this.data = data; }
}
static class LinkedList {
Node head;
public void deleteFromBeginning() {
if (head == null) return;
head = head.next;
}
public void add(int data) {
Node newNode = new Node(data);
newNode.next = head;
head = newNode;
}
public void printList() {
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
System.out.println();
}
}
public static void main(String[] args) {
LinkedList list = new LinkedList();
list.add(30);
list.add(20);
list.add(10);
System.out.println("Before deletion:");
list.printList();
list.deleteFromBeginning();
System.out.println("After deletion:");
list.printList();
}
}
Input:
10 20 30 (head at 10)
Output:
20 30
Space Complexity: O(1)
Comparison between Methods for Deletion from the beginning
| Method | Time Complexity | Space Complexity |
|---|---|---|
| Simple Pointer Update | O(1) | O(1) |
| Using LinkedList Wrapper Class | O(1) | O(1) |
In this article, you learned everything about Deletion from the Beginning in a Linked List in Java, including algorithms, code examples
and complexities. Since it requires only a single pointer update, it is one of the fastest and simplest linked list operations. Understanding it helps you build a strong foundation in linked list manipulation and prepares you for advanced operations.
FAQ's related to Deletion from the beginning in a Linked List in Java
Answer:
It is the process of removing the head node and pointing the head to the next node in the list.
Answer:
You update the head pointer to head.next and disconnect the old head.
Answer:
Yes, it takes constant time O(1) because only one pointer is changed.
Answer:
Yes, Java’s garbage collector releases memory of the removed node automatically.
Answer:
Yes, by repeatedly applying Deletion from the Beginning operation.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Singly Linked List
- Introduction to Linked List in Data Structure
Click Here - Linked List in –
- Singly Linked List in –
- Insertion in singly Linked List –
- Insertion at beginning in singly Linked List –
- Insertion at nth position in singly Linked List –
- Insertion at end in singly Linked List –
- Deletion in singly Linked List –
- Deletion from beginning in singly linked list :
- Deletion from nth position in singly linked list :
- Deletion from end in singly linked list :
- Linked List Insertion and Deletion –
C | C++ | Java - Reverse a linked list without changing links between nodes (Data reverse only) –
C | C++ | Java - Reverse a linked list by changing links between nodes –
- Print reverse of a linked list without actually reversing –
- Print reverse of a linked list without actually reversing –
- Insertion in the middle Singly Linked List –
- Insertion in a Sorted Linked List –
- Delete alternate nodes of a Linked List –
- Find middle of the linked list –
- Reverse a linked list in groups of given size –
- Find kth node from end of the linked list –
- Append the last n nodes of a linked list to the beginning of the list –
- Check whether linked list is palindrome or not –
- Fold a Linked List –
- Insert at given Position –
- Deletion at given Position –
Singly Linked List
- Introduction to Linked List in Data Structure
- Linked List in – C | C++ | Java
- Singly Linked List in – C | C++ | Java
- Insertion in singly Linked List – C | C++ | Java
- Deletion in singly Linked List – C | C++ | Java
- Reverse a linked list without changing links between nodes (Data reverse only) – C | C++ | Java
- Linked List Insertion and Deletion – C | C++ | Java
- Reverse a linked list by changing links between nodes – C | C++ | Java
- Linked List insertion in the middle – C | C++ | Java
- Print reverse of a linked list without actually reversing – C |C++ | Java
- Search an element in a linked list – C | C++ | Java
- Insertion in a Sorted Linked List – C | C++ | Java
- Delete alternate nodes of a Linked List – C | C++ | Java
- Find middle of the linked list – C | C++ | Java
- Reverse a linked list in groups of given size – C | C++ | Java
- Find kth node from end of the linked list – C | C++ | Java
- Append the last n nodes of a linked list to the beginning of the list – C | C++ | Java
- Check whether linked list is palindrome or not – C | C++ | Java
- Fold a Linked List – C | C++ | Java
- Insert at a given position – C | C++ | Java
- Delete at a given position – C | C++ | Java

Login/Signup to comment