How to insert a node at the beginning of a Doubly Linked List in Java ?

By | July 9, 2020

In this post, We will learn How to insert a node at the beginning of a Doubly Linked List in Java?

Logic is Very Simple:

Initially, We assign head  to the first pointer  and if  first is pointer  is null that means LinkedList is empty so  make newNode as tail Node so that head and tail  both pointing to the same Node  else newNode assign to first’s previous pointer in case LinkedList is not empty

Doubly Linked List

Below is the complete Source code:

LinkedList.java

 

ClientTest.java

The output of this Program:

—–Print Linked List in Forward Direction——-
30 20 10
—–Print Linked List in Backward Direction——-
10 20 30

 

You May Also Like:

How to represent the 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 insert a node at the beginning of 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.