Javatpoint Logo
Javatpoint Logo

Inserting a Node in a Doubly Linked List in Java

A doubly linked list is a data structure where each node contains two references, one pointing to the previous node and another pointing to the next node. This allows for efficient traversal in both directions. Inserting a node in a doubly linked list involves updating the references of the neighbouring nodes to maintain the correct order. In this article, we will explore how to insert a node in a doubly linked list using Java.

Let's start by defining the structure of a node in the doubly linked list:

Here, each node contains an integer value data, and references to the previous and next nodes (prev and next).

To insert a new node in a doubly linked list, we need to consider three cases:

  • Insertion at the beginning of the list.
  • Insertion at the end of the list.
  • Insertion at a specific position within the list.

Insertion at the beginning of the list:

To insert a node at the beginning, we need to update the next reference of the new node to point to the current first node, and update the prev reference of the current first node to point back to the new node. Additionally, we need to update the prev reference of the new node to be null, as it will be the new first node.

Insertion at the end of the list:

To insert a node at the end, we need to traverse the list until we reach the last node. Then, we update the next reference of the current last node to point to the new node, and update the prev reference of the new node to point back to the current last node. Finally, we update the next reference of the new node to be null, as it will be the new last node.

Insertion at a specific position within the list:

To insert a node at a specific position, we need to traverse the list until we reach the desired position. Then, we update the references of the neighbouring nodes to include the new node. Specifically, we update the next reference of the previous node to point to the new node, update the prev reference of the new node to point back to the previous node, update the next reference of the new node to point to the next node, and update the prev reference of the next node to point back to the new node.

InsertNodeExample.java

Output:

Doubly Linked List: 10 20 30
Doubly Linked List: 5 10 20 30
Doubly Linked List: 5 15 10 20 30

With these three insertion methods, you can easily add new nodes to a doubly linked list in Java. Remember to handle special cases such as an empty list or an invalid position.

In conclusion, the doubly linked list is a versatile data structure that allows for efficient insertion and traversal in both directions. By understanding how to insert a node in different positions within the list, you can utilize the doubly linked list effectively in your Java programs.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA