Java Program for deletion in a singly linked list. In the singly linked list, we can delete the node in the following ways or we can say they ways of deleting nodes . When we delete the node in the linked list then there are three ways to delete the node .
Methods to delete
There can be two different approaches for deletion –
import java.lang.*;
class LinkedList {
Node head;
// Node Class
class Node{
int data;
Node next;
Node(int x) // parameterized constructor
{
data = x;
next = null;
}
}
public Node insertStart(int data)
{
// Creating newNode memory & assigning data value
Node newNode = new Node(data);
// assigning this newNode's next as current head node
newNode.next = head;
// re-assigning head to this newNode
head = newNode;
return head;
}
public void display()
{
Node node = head;
//as linked list will end when Node reaches Null
while(node!=null)
{
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
void deleteVal(int value)
{
Node temp = head;
Node previous = null;
if(temp == null){
System.out.println("Can't delete Linked List empty");
return;
}
// Case when there is only 1 node in the list
if(temp.next==null)
{
head = null;
System.out.println("Deleted: " + value);
return;
}
// if the head node itself needs to be deleted
if(temp.data==value)
{
head = temp.next; //changing head to next in the list
System.out.println("Deleted: " + value);
return;
}
// traverse until we find the value to be deleted or LL ends
while (temp != null && temp.data != value)
{
// store previous link node as we need to change its next val
previous = temp;
temp = temp.next;
}
// if value is not present then
// temp will have traversed to last node NULL
if(temp==null)
{
System.out.println("Value not found");
return;
}
// for node to be deleted : temp lets call it nth node
// assign (n-1)th node's next to (n+1)th node
previous.next = temp.next;
System.out.println("Deleted: " + value);
}
}
public class Main
{
public static void main(String args[])
{
LinkedList ll = new LinkedList();
ll.insertStart(6);
ll.insertStart(5);
ll.insertStart(4);
ll.insertStart(3);
ll.insertStart(2);
ll.insertStart(1);
ll.display();
ll.deleteVal(10);
ll.deleteVal(5);
ll.deleteVal(2);
ll.display();
ll.deleteVal(1);
ll.display();
}
}
Output
1 2 3 4 5 6
Value not found
Deleted: 5
Deleted: 2
1 3 4 6
Deleted: 1
3 4 6
Method 2
This method uses static functions to call methods and head is passed in the function itself.
import java.lang.*;
// Node Class
class Node {
int data;
Node next;
Node(int x) // parameterized constructor
{
data = x;
next = null;
}
}
class Main
{
static Node insert(Node head, int data)
{
// Creating newNode memory & assigning data value
Node newNode = new Node(data);
// assigning this newNode's next as current head node
newNode.next = head;
// re-assigning head to this newNode
head = newNode;
return head;
}
static Node deleteVal(Node head, int value)
{
Node temp = head;
Node previous = null;
if(temp == null){
System.out.println("Can't delete Linked List empty");
return head;
}
// Case when there is only 1 node in the list
if(temp.next==null)
{
head = null;
System.out.println("Deleted: " + value);
return head;
}
// if the head node itself needs to be deleted
if(temp.data==value)
{
head = temp.next; //changing head to next in the list
System.out.println("Deleted: " + value);
return head;
}
// traverse until we find the value to be deleted or LL ends
while (temp != null && temp.data != value)
{
// store previous link node as we need to change its next val
previous = temp;
temp = temp.next;
}
// if value is not present then
// temp will have traversed to last node NULL
if(temp==null)
{
System.out.println("Value not found");
return head;
}
// for node to be deleted : temp lets call it nth node
// assign (n-1)th node's next to (n+1)th node
previous.next = temp.next;
System.out.println("Deleted: " + value);
return head;
}
static void display(Node node) {
//as linked list will end when Node is Null
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
public static void main(String args[])
{
Node head = null;
head = insert(head,6);
head = insert(head,5);
head = insert(head,4);
head = insert(head,3);
head = insert(head,2);
head = insert(head,1);
display(head);
head = deleteVal(head,10);
head = deleteVal(head,5);
head = deleteVal(head,2);
display(head);
head = deleteVal(head,1);
display(head);
}
}
Output
1 2 3 4 5 6
Value not found
Deleted: 5
Deleted: 2
1 3 4 6
Deleted: 1
3 4 6
Deletion in a Singly Linked List in Java (For a Position)
Deletion can be performed at the following positions –
Below we will write a program that will handle all the above cases in a single function, however, if you wish to write individual functions for each you can check the pages hyperlinked above.
import java.lang.*;
class LinkedList {
Node head;
// Node Class
class Node{
int data;
Node next;
Node(int x) // parameterized constructor
{
data = x;
next = null;
}
}
public Node insert(int data)
{
// Creating newNode memory & assigning data value
Node newNode = new Node(data);
// assigning this newNode's next as current head node
newNode.next = head;
// re-assigning head to this newNode
head = newNode;
return head;
}
public void display()
{
Node node = head;
//as linked list will end when Node reaches Null
while(node!=null)
{
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
void deletepos(int pos)
{
Node temp = head;
Node previous = null;
int size = calcSize(head);
if(pos < 1 || pos > size){
System.out.println("Enter valid position");
return;
}
//if the head node itself needs to be deleted
if(pos == 1){
//changing head to next in the list
head = temp.next;
System.out.println("Deleted Item: "+ temp.data);
return;
}
//run until we find the value to be deleted in the list
while (--pos > 0) {
// store previous link node as we need to change its next val
previous = temp;
temp = temp.next;
}
// (pos-1)th node's next assigned to (pos+1)nth node
previous.next = temp.next;
System.out.println("Deleted Item: "+ temp.data);
}
public int calcSize(Node node){
int size = 0;
// traverse to the last node each time incrementing the size
while(node!=null){
node = node.next;
size++;
}
return size;
}
public static void main(String args[])
{
LinkedList ll = new LinkedList();
ll.insert(60);
ll.insert(50);
ll.insert(40);
ll.insert(30);
ll.insert(20);
ll.insert(10);
ll.display();
ll.deletepos(1);
ll.display();
ll.deletepos(3);
ll.display();
ll.deletepos(4);
ll.display();
ll.deletepos(-2); // not valid as pos negative
ll.deletepos(10); // not valid as breaches size of Linked List
}
}
Output
10 20 30 40 50 60
Deleted Item: 10
20 30 40 50 60
Deleted Item: 40
20 30 50 60
Deleted Item: 60
20 30 50
Enter valid position
Enter valid position
Method 2
This method uses static functions to call methods and head is passed in the function itself.
import java.lang.*;
// Node Class
class Node {
int data;
Node next;
Node(int x) // parameterized constructor
{
data = x;
next = null;
}
}
class Main
{
static Node insert(Node head, int data)
{
// Creating newNode memory & assigning data value
Node newNode = new Node(data);
// assigning this newNode's next as current head node
newNode.next = head;
// re-assigning head to this newNode
head = newNode;
return head;
}
static Node deletepos(Node head, int pos)
{
Node temp = head;
Node previous = null;
int size = calcSize(head);
if(pos < 1 || pos > size){
System.out.println("Enter valid position");
return head;
}
//if the head node itself needs to be deleted
if(pos == 1){
//changing head to next in the list
head = temp.next;
System.out.println("Deleted Item: "+ temp.data);
return head;
}
//run until we find the value to be deleted in the list
while (--pos > 0) {
// store previous link node as we need to change its next val
previous = temp;
temp = temp.next;
}
// (pos-1)th node's next assigned to (pos+1)nth node
previous.next = temp.next;
System.out.println("Deleted Item: "+ temp.data);
return head;
}
static void display(Node node) {
//as linked list will end when Node is Null
while (node != null) {
System.out.print(node.data + " ");
node = node.next;
}
System.out.println();
}
public static int calcSize(Node node){
int size = 0;
// traverse to the last node each time incrementing the size
while(node!=null){
node = node.next;
size++;
}
return size;
}
public static void main(String args[])
{
Node head = null;
head = insert(head,60);
head = insert(head,50);
head = insert(head,40);
head = insert(head,30);
head = insert(head,20);
head = insert(head,10);
display(head);
head = deletepos(head,1);
display(head);
head = deletepos(head,3);
display(head);
head = deletepos(head,4);
display(head);
head = deletepos(head,-2); // not valid as pos negative
head = deletepos(head,10); // not valid as breaches size of Linked List1);
display(head);
}
}
Output
10 20 30 40 50 60
Deleted Item: 10
20 30 40 50 60
Deleted Item: 40
20 30 50 60
Deleted Item: 60
20 30 50
Enter valid position
Enter valid position
20 30 50
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Login/Signup to comment