Doubly Linked List in java

If you want to practice data structure and algorithm programs, you can go through Java coding interview questions.

In this post, we will see about Doubly LinkedList implementation in java.
We have already seen the implementation of singly linked list. You can consider this as an extension of Singly linked list.It is quite complex to implement it as compared to singly linked list.
In doubly linked list, Node has data and pointers to next node and previous node. First node’s previous points to null and Last node‘s next also points to null, so you can iterate over linked list in both direction with these next and previous pointers.
An example of Doubly Linked List:

Doubly Linked List in java

Node for doubly linked list can be presented as below:

As you can see, we have one more extra reference(Node prev) in case of doubly linked list.
Let’s say, you want to do insert First operation in case of Doubly linked list, it will look like as below:

Insert Doubly Linked List in java

Doubly linked list in java example

Let’s implement Doubly Linked List in java.

Create a java file named DoublyLinkedList.java.

Create another class named DoubleLinkedListMain as below:

When you run above program, you will get below output:

Printing Doubly LinkedList (head –> tail)
{ 1 }
{ 7 }
{ 6 }
{ 5 }
{ 2 }Printing Doubly LinkedList (tail –> head)
{ 2 }
{ 5 }
{ 6 }
{ 7 }
{ 1 }================
Printing Doubly LinkedList (head –> tail)
{ 1 }
{ 6 }
{ 5 }
{ 2 }

Printing Doubly LinkedList (tail –> head)
{ 2 }
{ 5 }
{ 6 }
{ 1 }

================
Printing Doubly LinkedList (head –> tail)
{ 6 }
{ 5 }

Printing Doubly LinkedList (tail –> head)
{ 5 }
{ 6 }

That’s all about Doubly linked list in java.

Related Posts

  • 26 September

    Print all subarrays of a given array

    Table of ContentsProblemSolution If you want to practice data structure and algorithm programs, you can go through 100+ data structure and algorithm programs. In this post, we will see how to print all subarrays of given array. Problem Print all print all subarrays of given array. For example: If array is {1,2,3} then you need […]

  • 08 November

    Dijkstra’s algorithm in java

    Table of ContentsProblemAlgorithmDijkstra’s algorithm example If you want to practice data structure and algorithm programs, you can go through Java coding interview questions. In this post, we will see Dijkstra algorithm for find shortest path from source to all other vertices. Problem You will be given graph with weight for each edge,source vertex and you need […]

  • 22 October

    Trie data structure in java

    If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post, we will see about trie data structure in java. What is Trie : Trie is data structure which stores data in such a way that it can be retrieved faster and improve the performance. […]

  • 12 October

    Implement Queue using Linked List in java

    If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post , we will see how to implement Queue using Linked List in java. Queue is abstract data type which demonstrates First in first out (FIFO) behaviour. We will implement same behaviour using Array. Although java provides implementation […]

  • 10 October

    Convert sorted array to balanced binary search tree

    Table of ContentsAlgorithm:Java code to convert sorted array to balanced binary search tree: If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post ,  we will see how to convert sorted array to balanced binary search tree. Algorithm: You might know that inorder traversal of […]

  • 23 September

    Sort a Stack using another stack

    Table of ContentsProblemSolution :Java codeComplete Java program to sort a stack using addition stack: If you want to practice data structure and algorithm programs, you can go through 100+ java coding interview questions. In this post,  we will see how to sort a stack using another stack. Problem Given a Stack,  you need to sort it […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.