Delete nth node in Linked List in Java

Java Program for Deletion from the nth position in a Linked List

Lets have a look on different methods that we can use to delete the nth node of a Singly Linked List in Java
Deletion from nth position in a singly linked list

Implementation

  • Take the Linked List input from the user and calculate the length of the Linked List
  • Accept the nth node that the user wants to delete
  • Check if the value of n is valid should not be negative or 0 and should not exceed the size of Linked List
  • If the user wants to delete the first node then just change the header to next in the Linked List
  • Else go ahead and traverse the Linked List to the nth node
  • Change the next of (n-1)th node to (n+1)th node
  • Done we have deleted the nth node its memory will automatically be deleted since there are no references to it.
Delete nth node in Linked List in Java
deletion of nth node in Singly Linked List in Java

Code In JAVA Programming Language 

The following method uses class object to call the non static member functions of the class

import java.lang.*;

class LinkedList {
    Node head;

    // Node Class
    class Node{
        int data;
        Node next;

        Node(int val)
        {
            data = val;
            next = null;
        }

    }

    public void deleteNthNode(int n)
    {
        int len = calcLen(head);

        // Can only insert after 1st position
        // Can't insert if position to insert is greater than size of Linked List
        if(n < 1 || n > len)
        {
            System.out.println("Can't delete\n");

        }
        else
        {
            if(n == 1)
            {
                // head has to be deleted
                System.out.println("Deleted: " + head.data);
                head = head.next;
                return;
            }
            // required to traverse
            Node temp = head;
            Node previous = null;

            // traverse to the nth node
            while(--n > 0) {
                previous = temp;
                temp = temp.next;
            }
            // assigned next node of the previous node to nth node's next
            previous.next = temp.next;
            System.out.println("Deleted: " + temp.data);
        }
    }
    public void insert(int data)
    {
        Node newNode = new Node(data);
        newNode.next = head;
        head = newNode;
    }

    public void showList()
    {
        Node temp = head;
        //as linked list will end when Node reaches Null
        while(temp!=null)
        {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
        System.out.println("\n");
    }

    public int calcLen(Node temp){
        int len = 0;

        while(temp!=null){
            temp = temp.next;
            len++;
        }
        return len;
    }

    public static void main(String args[])
    {
        LinkedList ll = new LinkedList();

        ll.insert(35);
        ll.insert(34);
        ll.insert(33);
        ll.insert(32);
        ll.insert(31);
        ll.insert(30);

        ll.showList();

        ll.deleteNthNode(3);
        ll.deleteNthNode(4);
        ll.showList();
    }
}

Output

30 31 32 33 34 35 

Deleted: 32
Deleted: 34
30 31 33 35 

The following method uses static functions that are called with header references passed in them

import java.lang.*;

// Node Class
class Node {
    int data;
    Node next;

    Node(int x) // parameterized constructor
    {
        data = x;
        next = null;
    }
}

class Main
{


    static Node deleteNthNode(int n, Node head) {
        int len = calcLen(head);

        // Can only insert after 1st position
        // Can't insert if position to insert is greater than size of Linked List
        if(n < 1 || n > len)
            System.out.println("Can't delete\n");

        else
        {
            if(n == 1)
            {
                // head has to be deleted
                System.out.println("Deleted: " + head.data);
                head = head.next;
                return head;
            }
            // required to traverse
            Node temp = head;
            Node previous = null;

            // traverse to the nth node
            while(--n > 0) {
                previous = temp;
                temp = temp.next;
            }
            // assigned next node of the previous node to nth node's next
            previous.next = temp.next;
            System.out.println("Deleted: " + temp.data);
        }
        return head;
    }
    public static Node insert(Node head, int data)
    {
        Node newNode = new Node(data);
        newNode.next = head;
        head = newNode;

        return head;
    }
    static void showList(Node temp) {

        //as linked list will end when Node is Null
        while (temp != null) {
            System.out.print(temp.data + " ");
            temp = temp.next;
        }
        System.out.println("\n");
    }

    static int calcLen(Node node){
        int len = 0;

        while(node!=null)
        {
            node = node.next;
            len++;
        }
        return len;
    }

    public static void main(String args[])
    {
        Node head = null;

        head = insert(head, 35);
        head = insert(head, 34);
        head = insert(head, 33);
        head = insert(head, 32);
        head = insert(head, 31);
        head = insert(head, 30);
        showList(head);


        head = deleteNthNode( 3, head);
        head = deleteNthNode( 4, head);
        showList(head);

    }
}

Output

30 31 32 33 34 35 

Deleted: 32
Deleted: 34
30 31 33 35