Java program for deletion in doubly linked list

Java Program for Deletion in Doubly Linked List

Now Since we’ve already learnt enough about the Insertion Operation in a Doubly Linked List, let’s learn about the Deletion operation. We have three kinds of deletion in singly linked list in the same way we have three kinds of deletion in doubly linked list are as follows:

  • Deletion at the beginning of the linked list.
  • Deletion at Nth node/in middle of the linked list.
  • Deletion at the end of the linked list.

 

deletion in doubly linked list

Deletion from the Beginning of a Doubly Linked List

For deletion at the beginning first we have to create a linked list and have to check if the list is empty or not. If it is empty then both head and tail points towards the new node . if it is not empty then we can delete our first node very easily.

Algorithm

  • if (head == null) // if ended loop closed
  • make ptr = head
  • make head = head->next
  • make head->prev = NULL
  • free the pointer node
  • END.
deletion in beginning in a doubly linked list

Deletion from the Specific Position of a Doubly Linked List

Deleting the nth node is as simple as it was in singly linked list. For deleting the node on nth node we have to go through the same process of deletion in beginning but there is a slight change this time we have to delete the nth node. So when you insert a function of deletion it should be for nth node.

Algorithm

  • if (head == null) // if ended
  • make temp = head so we can freely move our pointer
  • temp->data= node
  • make temp = temp->next  // loop ended
  • Make pointer ptr = temp->next
  • temp->next = ptr->next
  • make ptr->next->prev = temp
  • free temp node
  • END
Deletion from Nth position in a doubly linked list

Deletion from the End of a Doubly Linked List

Here we have to delete the node from the end. It requires the traversing of list in order to reach the last node of the doubly linked list and after that we can run loop to delete a node from the end of the doubly linked list.

Algorithm.

  • (head == null)  then return.
  • make temp = head
  • while temp ->next != NULL
  • make temp = temp->next // [loop ended]
  • make temp->previous->next = NULL
  • Free temp node
  • Return
Deletion from end in a doubly linked list

Java code for deletion in a doubly linked list

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Doubly Linked List

  • Introduction to Doubly Linked list in Data Structure
    Click Here
  • Doubly Linked List in –
    C | C++ | Java
  • Insertion in doubly linked list –
    C | C++ | Java
  • Insertion at beginning in doubly linked list –
    C | C++ | Java
  • Insertion at end in doubly linked list –
    C | C++ | Java
  • Insertion at nth node in doubly linked list –
    C | C++ | Java
  • Deletion in doubly linked list  –
    C | C++ | Java
  • Deletion from beginning in doubly linked list :
  • Deletion from nth in doubly linked list :
    C | C++ | Java
  • Deletion from end in doubly linked list :
    C | C++ | Java
  • Insertion and Deletion in a  doubly linked list :
    C | C++ | Java
  • Insertion in the middle in a  doubly linked list :
    C | C++ | Java

Doubly Linked List