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.

 

 

Also Read: Divide Integers without Division Arithmetic Operator in Java

 

Java Program To Delete A Node In Linked List

 

Input:

 

push(7)

 

push(8)

 

push(6)

 

push(1)

deleteNode(1)

 

Output:

 

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 && temp.data == key) {
            head = temp.next; // Changed head
            return;
        }

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

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

        // Unlink the node from linked list
        prev.next = temp.next;
    }

    /* Inserts a new Node at front of the list. */
    public void push(int new_data)
    {
        Node new_node = new Node(new_data);
        new_node.next = 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.data + " ");
            tnode = tnode.next;
        }
    }

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

        llist.push(6);
        llist.push(8);
        llist.push(7);
        llist.push(1);

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

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

        System.out.println(
            "\nLinked List after Deletion :");
        llist.printList();
    }
}

 

Ethix

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

Leave a Reply

Your email address will not be published.