Delete A Node In Linked List using Java

Technology and generation are growing up together, and the younger generation users are somewhat connected with tech and the internet all the time. Not to mention, today the whole world in this time of crisis is working over the internet. But to make these technologies, software, etc a software developer must have excellent problem-solving skills. In this world where the internet is the new fuel, one needs to be pretty sharp. And by sharp, for software developers, it means knowing how to automate real-world problems using computer programs. Data structures help a lot in this journey of logic building. So today we’re going to write a simple data structure program to Delete A Node In Linked List using Java.


What is A Linked List?


A linked list is a basic list of nodes containing different elements for all algorithms in data structures. Also, it’s pretty easy to understand.


What’s The Approach?


  • The main objective of deleting a linked list node is to free the memory allocated to a node so that it gets used instead of being wasted.


  • To delete a given node firstly we’ll have to search for the node before it, as the previous node holds the address of the node we want to delete.


  • Once we find the previous node, change its address section to hold the address of our to be deleted node is currently holding in its address section.


  • Once the above instructions are done being executed, we’ll free the memory allocated to the deleted node.


Also Read: Divide Integers without Division Arithmetic Operator in Java


Java Program To Delete A Node In Linked List















Created Linked list is:

1 7 8 6 

Linked List after Deletion :

7 8 6



// Java program
// demonstrate deletion
// in linked list

class LinkedList {
    Node head; // head of list

    /* Linked list Node*/
    class Node {
        int data;
        Node next;
        Node(int d)
            data = d;
            next = null;

    /* Given a key, deletes the first
    occurrence of key in
    * linked list */
    void deleteNode(int key)
        // Store head node
        Node temp = head, prev = null;

        // If head node itself holds the key to be deleted
        if (temp != null && == key) {
            head =; // Changed head

        // Search for the key to be deleted, keep track of
        // the previous node as we need to change
        while (temp != null && != key) {
            prev = temp;
            temp =;

        // If key was not present in linked list
        if (temp == null)

        // Unlink the node from linked list =;

    /* Inserts a new Node at front of the list. */
    public void push(int new_data)
        Node new_node = new Node(new_data); = head;
        head = new_node;

    /* This function prints contents of linked list starting
    from the given node */
    public void printList()
        Node tnode = head;
        while (tnode != null) {
            System.out.print( + " ");
            tnode =;

    /* Driver program  */
    public static void main(String[] args)
        LinkedList llist = new LinkedList();


        System.out.println("\nCreated Linked list is:");

        llist.deleteNode(1); // Delete node with data 1

            "\nLinked List after Deletion :");



I'm a coding geek interested in cyberspace who loves to write and read

Leave a Reply

Your email address will not be published. Required fields are marked *