JAVA Program for Deletion from Nth Position of a Doubly Linked List

JAVA Program to Delete a Node from the Nth Position of a Doubly Linked Last

In this page we’ll take a look at a comprehensive explanation of Deleting a Node from a specific index of a Doubly Linked List, one of the most widely used operation on a Doubly Linked List

Example: If we have a list (10 –> 20 –> 30 –> 40 –> 50) and we have to delete a node on the Second Index of the List, that is i=2, then after deleting a  node. Updated Linked list will be (10 –> 20  –> 40 –> 50) . We will be learning more about the whole process in the explanation below.

JAVA Program to Delete a Node from the nth position of a doubly Linked List

Steps to be followed while Deleting a Node from the Nth Position of a Doubly Linked List

  • Check for the presence of Node in the List, if there exists some Nodes, Continue.
  • Now, to delete a node from Nth position of the Doubly Linked List, we’ll have to delete and redirect various links of the Linked List.
  • First of all the next pointer of the (n-1)th Node of the Linked List will now point to the address of the (n+1)th node of the Doubly Linked List.
  • Now the Previous Pointer of the (n+1)th Node of the Linked List will now be re-directed to the address of (n-1)th node of the List.
JAVA Program for Deletion from the nth node in a Doubly Linked List

Algorithm to be used for the Deletion of a Node from a Specific Index of a Doubly Linked List

  •  IF(HEAD == NULL)
    • RETURN
  • ELSE
    • NODE CURRENT = HEAD;
    • INT POS =N;
  • FOR(INT I = 1; I < POS; I++)
    • CURRENT = CURRENT.NEXT
  • IF(CURRENT == HEAD)
    • HEAD = CURRENT.NEXT
  • ELSE IF(CURRENT == TAIL)
    • TAIL = TAIL.PREV
  • ELSE
    • CURRENT.PREV.NEXT = CURRENT.NEXT
    • CURRENT.NEXT.PREV = CURRENT.PREV
  • CURRENT = NULL

Java Program for Deletion from the Nth Position of a Doubly Linked List

   public class PrepInsta
    {  
         class Node{  
            int data;  
            Node prev;  
            Node next;  
            public Node(int data) {  
                this.data = data;  
            }  
        }  
    public void display() {
        Node temp = head;
        while (temp != null) {
            System.out.print(temp.data + "–>");
            temp = temp.next;
        }
        System.out.println("END");
    }
    Node head, tail = null;  
    public void addNode(int data) {  
        Node newNode = new Node(data);  
         if(head == null) {  
            head = tail = newNode;  
            head.prev = null;  
            tail.next = null;  
        }  
        else {  
            tail.next = newNode;  
            newNode.prev = tail;  
            tail = newNode;  
            tail.next = null;  
        }  
    }
    public void deletenth(int n) {  
         if(head == null) {  
            return;  
        }  
        else {  
            Node current = head;  
  
              int pos =n;  
  
            for(int i = 1; i < pos; i++){  
                current = current.next;  
            }  
  
            if(current == head) {  
                head = current.next;  
            }  
             else if(current == tail) {  
                tail = tail.prev;  
            }  
            else {  
                current.prev.next = current.next;  
                current.next.prev = current.prev;  
            }  
             current = null;  
        }  
    }  
     void print() {  
        Node curr = head;  
        if(head == null) {  
            System.out.println("List is empty");  
            return;  
        }
        while(curr != null) 
           {  
            System.out.print(curr.data + " ");  
            curr = curr.next;  
            }  
        System.out.println();  
        }  
    public static void main(String[] args) {  
        PrepInsta dList = new PrepInsta(); 
        dList.addNode(10);  
        dList.addNode(20);  
        dList.addNode(30);
        dList.addNode(40);
        dList.addNode(50);
        System.out.println("Initial Doubly Linked List: "); 
        dList.print();
        dList.deletenth(2);
        System.out.println("Doubly Linked List after Deletion from nth Position: "); 
        dList.print();  
    }  
}
Initial Doubly Linked List: 
10 20 30 40 50 
Doubly Linked List after Deletion from nth Position: 
10 30 40 50