Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Interview Problems on Linked List
  • Practice Linked List
  • MCQs on Linked List
  • Linked List Tutorial
  • Types of Linked List
  • Singly Linked List
  • Doubly Linked List
  • Circular Linked List
  • Circular Doubly Linked List
  • Linked List vs Array
  • Time & Space Complexity
  • Advantages & Disadvantages
Open In App
Next Article:
Implementation of Doubly Linked List in JavaScript
Next article icon

Introduction to Doubly Linked Lists in Java

Last Updated : 25 Apr, 2023
Comments
Improve
Suggest changes
Like Article
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.

Java
public class Node {     int data;     Node prev;     Node next;      public Node(int data)     {         this.data = data;         this.prev = null;         this.next = null;     } } 

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.

Java
public class DoublyLinkedList {     Node head;     Node tail;      public DoublyLinkedList()     {         this.head = null;         this.tail = 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:

Java
// Traversing from head to the end of the list public void traverseForward() {     Node current = head;     while (current != null) {         System.out.print(current.data + " ");         current = current.next;     } } // Traversing from tail to the head public void traverseBackward() {     Node current = tail;     while (current != null) {         System.out.print(current.data + " ");         current = current.prev;     } } 

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:

Java
public void insertAtBeginning(int data) {     Node temp = new Node(data);     if (head == null) {         head = temp;         tail = temp;     }     else {         temp.next = head;         head.prev = temp;         head = temp;     } } 

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:

Java
public void insertAtPosition(int data, int position) {     Node temp = new Node(data);     if (position == 1) {         insertAtBeginning(data);     }     else {         Node current = head;         int currPosition = 1;         while (current != null && currPosition < position) {             current = current.next;             currPosition++;         }         if (current == null) {             insertAtEnd(data);         }         else {             temp.next = current;             temp.prev = current.prev;             current.prev.next = temp;             current.prev = temp;         }     } } 

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:

Java
public void insertAtEnd(int data) {     Node temp = new Node(data);     if (tail == null) {         head = temp;         tail = temp;     }     else {         tail.next = temp;         temp.prev = tail;         tail = temp;     } } 

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:

Java
public void deleteAtBeginning() {     if (head == null) {         return;     }      if (head == tail) {         head = null;         tail = null;         return;     }      Node temp = head;     head = head.next;     head.prev = null;     temp.next = null; } 

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:

Java
public void delete(int pos) {     if (head == null) {         return;     }      if (pos == 1) {         deleteAtBeginning();         return;     }      Node current = head;     int count = 1;      while (current != null && count != pos) {         current = current.next;         count++;     }      if (current == null) {         System.out.println("Position wrong");         return;     }      if (current == tail) {         deleteAtEnd();         return;     }      current.prev.next = current.next;     current.next.prev = current.prev;     current.prev = null;     current.next = null; } 

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:

Java
public void deleteAtEnd() {     if (tail == null) {         return;     }      if (head == tail) {         head = null;         tail = null;         return;     }      Node temp = tail;     tail = tail.prev;     tail.next = null;     temp.prev = null; } 

Below is the code to test the above functions:

Java
// Java program for the above approach import java.io.*;  class node {     node prev;     int data;     node next;      // A constructor is called here     node(int value)     {          // By default previous pointer is         // pointed to NULL         prev = null;          // Value is assigned to the data         data = value;          // By default next pointer is pointed         // to NULL         next = null;     } }  class GFG {      // Declaring an empty doubly linked list     static node head = null;     static node tail = null;     static void insertAtBeginning(int data)     {         node temp = new node(data);         if (head == null) {             head = temp;             tail = temp;         }         else {             temp.next = head;             head.prev = temp;             head = temp;         }     }      static void insertAtEnd(int data)     {         node temp = new node(data);         if (tail == null) {             head = temp;             tail = temp;         }         else {             tail.next = temp;             temp.prev = tail;             tail = temp;         }     }      static void insertAtPosition(int data, int position)     {         node temp = new node(data);         if (position == 1) {             insertAtBeginning(data);         }         else {             node current = head;             int currPosition = 1;             while (current != null                    && currPosition < position) {                 current = current.next;                 currPosition++;             }             if (current == null) {                 insertAtEnd(data);             }             else {                 temp.next = current;                 temp.prev = current.prev;                 current.prev.next = temp;                 current.prev = temp;             }         }     }      static void deleteAtBeginning()     {         if (head == null) {             return;         }          if (head == tail) {             head = null;             tail = null;             return;         }          node temp = head;         head = head.next;         head.prev = null;         temp.next = null;     }      static void deleteAtEnd()     {         if (tail == null) {             return;         }          if (head == tail) {             head = null;             tail = null;             return;         }          node temp = tail;         tail = tail.prev;         tail.next = null;         temp.prev = null;     }      static void deleteAtSpecificPosition(int pos)     {         if (head == null) {             return;         }          if (pos == 1) {             deleteAtBeginning();             return;         }          node current = head;         int count = 1;          while (current != null && count != pos) {             current = current.next;             count++;         }          if (current == null) {             System.out.println("Position wrong");             return;         }          if (current == tail) {             deleteAtEnd();             return;         }          current.prev.next = current.next;         current.next.prev = current.prev;         current.prev = null;         current.next = null;     }      static void display(node head)     {         node temp = head;         while (temp != null) {             System.out.print(temp.data + " --> ");             temp = temp.next;         }         System.out.println("NULL");     }      // Drivers code     public static void main(String[] args)     {         insertAtEnd(1);         insertAtEnd(2);         insertAtEnd(3);         insertAtEnd(4);         insertAtEnd(5);          System.out.print("After insertion at tail: ");         display(head);          System.out.print("After insertion at head: ");         insertAtBeginning(0);         display(head);          insertAtPosition(6, 2);         System.out.print(             "After insertion at 2nd position: ");         display(head);          deleteAtBeginning();         System.out.print(             "After deletion at the beginning: ");         display(head);          deleteAtEnd();         System.out.print("After deletion at the end: ");         display(head);          deleteAtSpecificPosition(2);         System.out.print(             "After deletion at 2nd position: ");         display(head);     } } 

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
Implementation of Doubly Linked List in JavaScript
author
zaidkhan15
Improve
Article Tags :
  • Linked List
  • DSA
  • doubly linked list
Practice Tags :
  • Linked List

Similar Reads

  • Doubly Linked List meaning in DSA
    A doubly linked list is a special type of linked list in which each node contains a pointer to the previous node as well as the next node in the structure. Characteristics of the Doubly Linked List: The characteristics of a doubly linked list are as follows: Dynamic size: The size of a doubly linked
    3 min read
  • Doubly Linked List Tutorial
    A doubly linked list is a more complex data structure than a singly linked list, but it offers several advantages. The main advantage of a doubly linked list is that it allows for efficient traversal of the list in both directions. This is because each node in the list contains a pointer to the prev
    9 min read
  • Difference between Singly linked list and Doubly linked list
    Introduction to Singly linked list : A singly linked list is a set of nodes where each node has two fields 'data' and 'link'. The 'data' field stores actual piece of information and 'link' field is used to point to next node. Basically the 'link' field stores the address of the next node.  Introduct
    2 min read
  • Applications, Advantages and Disadvantages of Doubly Linked List
    Doubly linked list is a type of linked list in which nodes contains information and two pointers i.e. left pointer and right pointer. The left pointer in the doubly linked list points to the previous node and the right pointer points to the next node in the linked list. The first node of the doubly
    4 min read
  • Operations on Doubly Linked

    • Operations of Doubly Linked List with Implementation
      A Doubly Linked List (DLL) contains an extra pointer, typically called the previous pointer, together with the next pointer and data which are there in a singly linked list. Below are operations on the given DLL: Add a node at the front of DLL: The new node is always added before the head of the giv
      15+ min read

    • Insertion in a Doubly Linked List
      Inserting a new node in a doubly linked list is very similar to inserting new node in linked list. There is a little extra work required to maintain the link of the previous node. In this article, we will learn about different ways to insert a node in a doubly linked list. Table of Content Insertion
      6 min read

    • Search an element in a Doubly Linked List
      Given a Doubly linked list(DLL) containing n nodes and an integer x, the task is to find the position of the integer x in the doubly linked list. If no such position found then print -1. Examples: Input: Linked List = 18 <-> 15 <-> 8 <-> 9 <-> 14, x = 8 Output: 3 Explanation:
      7 min read

    • Deletion in a Doubly Linked List
      Deleting a node in a doubly linked list is very similar to deleting a node in a singly linked list. However, there is a little extra work required to maintain the links of both the previous and next nodes. In this article, we will learn about different ways to delete a node in a doubly linked list.
      15+ min read

    • Delete a Doubly Linked List node at a given position
      Given a doubly linked list and a position pos, the task is to delete the node at the given position from the beginning of Doubly Linked List. Input: LinkedList: 1<->2<->3, pos = 2Output: LinkedList: 1<->3 Input: LinkedList: 1<->2<->3, pos = 1Output: LinkedList: 2<-
      9 min read

    Doubly Linked List in Different Languages

    • How to Create a Doubly Linked List in C?
      A doubly linked list is a type of linked list in which each node contains a pointer to both the next node and the previous node. This allows traversal in both forward and backward directions. Each node in a doubly linked list stores data, a pointer to the next node, and a pointer to the previous nod
      4 min read
    • Introduction to Doubly Linked Lists in Java
      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 nodene
      11 min read
    • Implementation of Doubly Linked List in JavaScript
      This article will demonstrate the Implementation of Doubly Linked List In JavaScript. A doubly linked list (DLL) is a special type of linked list in which each node contains a pointer to the previous node as well as the next node of the linked list. Doubly Linked List in JavaScriptTo create we have
      4 min read
    • Memory efficient doubly linked list
      We need to implement a doubly linked list with the use of a single pointer in each node. For that we are given a stream of data of size n for the linked list, your task is to make the function insert() and getList(). The insert() function pushes (or inserts at the beginning) the given data in the li
      9 min read
    geeksforgeeks-footer-logo
    Corporate & Communications Address:
    A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
    Registered Address:
    K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
    GFG App on Play Store GFG App on App Store
    Advertise with us
    • Company
    • About Us
    • Legal
    • Privacy Policy
    • In Media
    • Contact Us
    • Advertise with us
    • GFG Corporate Solution
    • Placement Training Program
    • Languages
    • Python
    • Java
    • C++
    • PHP
    • GoLang
    • SQL
    • R Language
    • Android Tutorial
    • Tutorials Archive
    • DSA
    • Data Structures
    • Algorithms
    • DSA for Beginners
    • Basic DSA Problems
    • DSA Roadmap
    • Top 100 DSA Interview Problems
    • DSA Roadmap by Sandeep Jain
    • All Cheat Sheets
    • Data Science & ML
    • Data Science With Python
    • Data Science For Beginner
    • Machine Learning
    • ML Maths
    • Data Visualisation
    • Pandas
    • NumPy
    • NLP
    • Deep Learning
    • Web Technologies
    • HTML
    • CSS
    • JavaScript
    • TypeScript
    • ReactJS
    • NextJS
    • Bootstrap
    • Web Design
    • Python Tutorial
    • Python Programming Examples
    • Python Projects
    • Python Tkinter
    • Python Web Scraping
    • OpenCV Tutorial
    • Python Interview Question
    • Django
    • Computer Science
    • Operating Systems
    • Computer Network
    • Database Management System
    • Software Engineering
    • Digital Logic Design
    • Engineering Maths
    • Software Development
    • Software Testing
    • DevOps
    • Git
    • Linux
    • AWS
    • Docker
    • Kubernetes
    • Azure
    • GCP
    • DevOps Roadmap
    • System Design
    • High Level Design
    • Low Level Design
    • UML Diagrams
    • Interview Guide
    • Design Patterns
    • OOAD
    • System Design Bootcamp
    • Interview Questions
    • Inteview Preparation
    • Competitive Programming
    • Top DS or Algo for CP
    • Company-Wise Recruitment Process
    • Company-Wise Preparation
    • Aptitude Preparation
    • Puzzles
    • School Subjects
    • Mathematics
    • Physics
    • Chemistry
    • Biology
    • Social Science
    • English Grammar
    • Commerce
    • World GK
    • GeeksforGeeks Videos
    • DSA
    • Python
    • Java
    • C++
    • Web Development
    • Data Science
    • CS Subjects
    @GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
    We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
    Lightbox
    Improvement
    Suggest Changes
    Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
    geeksforgeeks-suggest-icon
    Create Improvement
    Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
    geeksforgeeks-improvement-icon
    Suggest Changes
    min 4 words, max Words Limit:1000

    Thank You!

    Your suggestions are valuable to us.

    What kind of Experience do you want to share?

    Interview Experiences
    Admission Experiences
    Career Journeys
    Work Experiences
    Campus Experiences
    Competitive Exam Experiences