How to delete the last node in a Doubly Linked List in Java?

By | July 10, 2020

In the previous post, How to delete the first node in a Doubly Linked List in Java?  we learned how to delete the first node from LinkedList.

In this post, We will learn How to delete the last node in a Doubly Linked List in Java?

Logic is Very Simple:

  1. if List is Empty than throws NoSuchElementException Exception
  2. if List is not empty then tail we will assign to temp Variable 
    1. if  head ==  tail that means list has only one Node so nullify head node
    2. if List has more than one Node then nullify tail.previous.next, so that tail’s previous and next pointer disconnects.

      3.  Afterward, the tail’s move to the previous pointer and temp’s previous pointer nullify.

LinkedList.java

ClientTest.java

The output of this Program:

—–Original LinkedList——-
10 20 30
Removed Element::30
———-After Deleting Last Node from Linked List—————
10 20

You May Also Like:

How to represent the Doubly Linked List in Java?
How to insert a node at the beginning of a Doubly Linked List in Java ?
How to insert a node at the end of a Doubly Linked List in Java ?
How to delete the first node in a Doubly Linked List in Java?
Representation of Singly Linked List in Java ?
How to insert a node at the beginning of a Singly Linked List in Java?
How to Insert node at the end of a Singly Linked List in Java ?
How to insert a node in Linked List at a given position in Java ?
How to remove the first node from a Singly Linked List in Java ?
How to remove the last node from a Singly Linked List in Java
How to remove a node from a Singly Linked List at a given position in Java?
How to remove a given key from the Singly Linked List in Java ?
How to find the middle node in a Singly Linked List in Java ?
How to search an element in a Singly Linked List in Java ?

That’s all about the How to delete the last node in a Doubly Linked List in Java?
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.