Open In App

Introduction to Doubly Linked Lists in Java

Last Updated : 25 Apr, 2023
Summarize
Comments
Improve
Suggest changes
Share
8 Likes
Like
Report

Doubly linked list is a data structure that has reference to both the previous and next nodes in the list. It provides simplicity to traverse, insert and delete the nodes in both directions in a list.

In a doubly linked list, each node contains three data members:

  • data: The data stored in the node
  • next: It refers to the reference to the next node
  • prev: It refers to the reference to the previous node

Creation of Doubly Linked Lists in Java:

To create a doubly linked list, first, we need to define a Node class that has three data members that will store the data stored in the node, the reference to the next node, and the reference to the previous node.

Now we need to create a doubly linked list class that will contain two variables head and tail which will be referenced to the first and last node of the list respectively and also a constructor which will initialize both head and tail to the null.

Traversing a Doubly Linked List in Java:

To traverse in a doubly linked list, we will start from the head node and follow the next reference until we reach the end of the list similarly, we can also start from the tail node and follow the prev until we reach the head of the node.

Below is the code to traverse a linked list:

Insertion in a Doubly Linked List in Java:

To insert a node in a doubly linked list, first, we will need to create a new node that is going to be inserted and update the references of the adjacent nodes to add the new node and will update the head or tail of the list if the new node is being inserted at the beginning or end of the list.

A node can be added to a Doubly Linked List in three ways:

  • Insertion at the beginning of the list
  • Insertion at a specific position in the list
  • Insertion at the end of the list

1) Insertion at the beginning of the list:

Create a new node to be inserted. Set the next pointer of the new node to the current head of the doubly linked list. Set the previous pointer of the new node to null, as it will be the new head. If the doubly linked list is not empty (i.e., the current head is not null), set the previous pointer of the current head to the new node.

 Insertion at the beginning of the list
 Insertion at the beginning of the list

Below is the code to insert a node at the front of the linked list:

2) Insertion at a specific position in the list:

Create a new node with the data to be inserted. Set the next pointer of the new node to the next node of the node at given position. Set the previous pointer of the new node to the node at given position. Set the next pointer of the node at the given position to the new node. If the next node of the new node is not null, set its previous pointer to the new node.

Insertion at a specific position in the list
Insertion at a specific position in the list

Below is the code to insert a node at a specific position in the linked list:

3) Insertion at the end of the list:

First, we create a new node temp with the given data and then we will check if the list is empty or not. If it is empty then we will set both the head and tail to the new node temp, and if the list is not empty, traverse to the last node of the doubly linked list. Set the next pointer of the last node to the new node. Set the previous pointer of the new node to the current last node. Set the next pointer of the new node to null, as it will be the last node in the list.

Insertion at the end of the list
Insertion at the end of the list

Below is the code to insert a node at the end of the linked list:

Deletion in a Doubly Linked List in Java:

To delete a node in a doubly linked list, first, we need to update the references of the adjacent nodes to delete the node and will update the head or tail of the list if the new node is being inserted at the beginning or end of the list.

The deletion of a node in a doubly-linked list can be divided into three main categories:

  • Deletion of the first node in the list.
  • Deletion of a node at a specific position in the list.
  • Deletion of the last node in the list.

Example:

 

1) Deletion of the first node in the list:

First, we will check if the list is empty if it is then we return, and if there is only one node in the list then we will set both the head and tail to null otherwise we will simply set the head node to the next node of the current head in the list, and set the previous reference of the new head node to null.

  • After the deletion of the first node.
Deletion of the first node in the list
Deletion of the first node in the list

Below is the code of Deletion of the first node in the list:

2) Deletion of a node at a specific position in the list:

If the given position is 1 (i.e., the first node), update the head of the doubly linked list to be the next node of the current head. Traverse to the node at the given position, keeping track of the previous node. Set the next pointer of the previous node to the next node of the node to be deleted. If the next node of the node to be deleted is not null, set its previous pointer to the previous node. Delete the node.

  • After the deletion of the 2nd node.
Deletion of a node at a specific position in the list
Deletion of a node at a specific position in the list

Below is the code of Deletion of a node at a specific position in the list:

3) Deletion of the last node in the list:

First, we will check if the list is empty if it is then we return, and if there is only one node in the list then we will set both the head and tail to null otherwise we will simply set the tail node to the previous node of the current tail node in the list, set the prev of the current tail node as null and set the next reference of the new tail node to null.

Deletion of the last node in the list
Deletion of the last node in the list

Below is the code for the Deletion of the last node in the list:

Below is the code to test the above functions:


Output
After insertion at tail: 1 --> 2 --> 3 --> 4 --> 5 --> NULL
After insertion at head: 0 --> 1 --> 2 --> 3 --> 4 --> 5 --> NULL
After insertion at 2nd position: 0 --> 6 --> 1 --> 2 --> 3 --> 4 --> 5 --> NULL
After deletion at the beginning: 6 --> 1 --> 2 --> 3 --> 4 --> 5 --> NULL
After deletion at the end: 6 --> 1 --> 2 --> 3 --> 4 --> NULL
After deletion at 2nd position: 6 --> 2 --> 3 --> 4 --> NULL

Next Article
Practice Tags :

Similar Reads