Singly Linked List Deletion in Java

Singly linked list Deletion

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 as follows.

  • Deletion at the beginning
  • Deletion at middle or n node.
  • Deletion at last

Methods to delete

There can be two different approaches for deletion –

Singly Linked List Deletion in Java
Singly Linked List deletion in Java value

Linked List Deletion in Java (For a Value)

This method uses Linked List objects to call non static functions via object
Run
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
This method uses static functions to call methods and head is passed in the function itself.
Run
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.

This method uses Linked List objects to call non static functions via object

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

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 

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • 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 –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly 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 given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

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
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end 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