Delete a node at a specific position in a Linked List in Java
Java Program to Delete a node from a given position
We will write a Program to delete a node at a specific position in a Linked List in Java.
let us look at how we can delete a specific node in linked list in Java
Method to delete a specific node in Linked List Java
- For the nth n node that the user may want to delete
- Visit the node just previous i.e. (n-1) node to it and change its next reference to the (n+1)th node
Also, write a separate case if the first node has to be deleted since the head reference will be changed in this case.
Program to Insert a node at given position in linked list in Java
Run
// delete a specific node in linked list java
import java.lang.*;
class LinkedList {
Node head;
int size = 0;
// Node Class
class Node{
int data;
Node next;
Node(int x) // parameterized constructor
{
data = x;
next = null;
}
}
public void insert(int data)
{
Node new_node = new Node(data);
new_node.data = data;
new_node.next = head;
head = new_node;
size++;
}
public void deletePosition(int pos) {
Node temp = head;
Node prevNode = null;
if(pos < 1 || pos > size){
System.out.println("Invalid\n");
return;
}
// delete the 1st node
if(pos == 1){
head = head.next;
System.out.println("Deleted: " + temp.data);
size--;
return;
}
// traverse to the pos'th node
while (--pos > 0)
{
prevNode = temp;
temp = temp.next;
}
// change prevNode node's next node to nth node's next node
prevNode.next = temp.next;
// delete this nth node
System.out.println("Deleted: " + temp.data);
size--;
}
public void display()
{
System.out.print("Linked List : ");
Node node = head;
// as linked list will end when Node is Null
while(node!=null){
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
}
public class Main{
public static void main(String args[])
{
LinkedList linked_list = new LinkedList();
linked_list.insert(5);
linked_list.insert(4);
linked_list.insert(3);
linked_list.insert(2);
linked_list.insert(1);
linked_list.display();
// deletes the 2nd node
linked_list.deletePosition(2);
linked_list.display();
// deletes the 3rd node
linked_list.deletePosition(3);
linked_list.display();
}
}Output
Linked List : 1 2 3 4 5 Deleted: 2 Linked List : 1 3 4 5 Deleted: 4 Linked List : 1 3 5
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
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