In this Linked List tutorial, we will see all about the delete operation in a single linked list. In the delete operation, we will remove an existing node from the linked list from any position.
In a Singly Linked List, we can delete a node at the following positions:
From the beginning of the linked list.
From middle of the linked list.
From the end of the linked list.
Create a node class
public class Node {
public int data;
public Node next;
// Constructor
public Node(int data) {
this.data = data;
this.next = null;
}
}
1. Delete a node from the beginning of the linked list
In the below given example we will see how to delete an existing node from the beginning or front the linked list.
// Java program to delete a node from the beginning of the linked list
public class DeleteNodeAtBignningInSLL {
// Method to delete the head node
static Node deleteHead(Node head) {
// Before deleting a node just make sure list should not be empty
if (head == null) {
System.out.println("List is empty");
return null;
}
// Change the head pointer to the next node
// and free the original head
Node temp = head;
head = head.next;
// Free the original head (handled by garbage collector)
temp = null;
// Return the new head
return head;
}
// Method to print the doubly linked list
public static void printList(Node head) {
while (head != null) {
System.out.print(head.data + " -> ");
head = head.next;
}
System.out.println("null");
}
public static void main(String[] args) {
// Create nodes
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
System.out.println("Original Linked List data: ");
printList(head);
// Deleting the head node
head = deleteHead(head);
System.out.println("After deleting head, Linked List data: ");
printList(head);
}
}
Original Linked List data: 10 -> 20 -> 30 -> 40 -> null After deleting head, Linked List data: 20 -> 30 -> 40 -> null
2. Delete a node from the middle of the linked list
In the below given example we will see how to delete an existing node from the any position of the linked list.
// Java program to delete a node from the linked list at the given position
public class DeleteNodeAtMiddleInSLL {
// Method to delete a node at a given position
public static Node deleteNode(Node head, int position) {
// Before deleting a node just make sure list should not be an empty
if (head == null) {
System.out.println("List is empty");
return head;
}
Node temp = head;
Node prev = null;
// Case 1: Head is to be deleted
if (position == 1) {
head = temp.next;
return head;
}
// Case 2: Node to be deleted is in middle
// Traverse till given position
for (int i = 1; temp != null && i < position; i++) {
prev = temp;
temp = temp.next;
}
// If given position is found, delete node
if (temp != null) {
prev.next = temp.next;
} else {
System.out.println("Data not present in the list.");
}
return head;
}
// Method to print a list
public static void printList(Node head) {
while (head != null) {
System.out.print(head.data + " -> ");
head = head.next;
}
System.out.println("null");
}
public static void main(String[] args) {
// Create nodes
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
head.next.next.next.next = new Node(50);
System.out.println("Original Linked List data:");
printList(head);
// Deleting node at position 3
head = deleteNode(head, 3);
// Print list after deletion
System.out.print("After deletion, Linked List data:\n");
printList(head);
}
}
Original Linked List data: 10 -> 20 -> 30 -> 40 -> 50 -> null After deletion, Linked List data: 10 -> 20 -> 40 -> 50 -> null
3. Delete a node from the end of the linked list
In the below given example we will see how to delete an existing node from the end or last the linked list.
// Java program to delete the end or last node of linked list
public class DeleteNodeAtEndInSLL {
// Method to remove the last node from the linked list
static Node removeLastNode(Node head) {
// Before deleting a node just make sure list should not be empty
if (head == null) {
System.out.println("List is empty");
return null;
}
// If the list has only one node, delete it and return null
if (head.next == null) {
return null;
}
// Find the second last node
Node secondLast = head;
while (secondLast.next.next != null) {
secondLast = secondLast.next;
}
// Delete the last node
secondLast.next = null;
return head;
}
// Method to print the list
public static void printList(Node head) {
while (head != null) {
System.out.print(head.data + " -> ");
head = head.next;
}
System.out.println("null");
}
public static void main(String[] args) {
// Create nodes
Node head = new Node(10);
head.next = new Node(20);
head.next.next = new Node(30);
head.next.next.next = new Node(40);
System.out.println("Original Linked List data: ");
printList(head);
// Removing the last node from the list
head = removeLastNode(head);
System.out.println("After removing last node, Linked List data: ");
printList(head);
}
}
Original Linked List data: 10 -> 20 -> 30 -> 40 -> null After removing last node, Linked List data: 10 -> 20 -> 30 -> null