Skip to main content

Insert node at beginning of doubly linked list java

Java program for Insert node at beginning of doubly linked list. Here problem description and explanation.

// Java Program For
// insert new node at beginning of doubly linked list

// Define class of linked list Node
class LinkNode
    public int data;
    public LinkNode next;
    public LinkNode prev;
    public LinkNode(int data)
    { = data; = null;
        this.prev = null;
public class DoublyLinkedList
    public LinkNode head;

    public DoublyLinkedList()
        // Set inital value
        this.head = null;
    // Insert new node at beginning position
    public void insert(int value)
        // Create a node
        LinkNode node = new LinkNode(value); = this.head;
        // When linked list is not empty
        if (this.head != null)
            this.head.prev = node;
        // Make new head
        this.head = node;
    // Display node element of doubly linked list
    public void display()
        if (this.head == null)
            System.out.println("Empty Linked List");
            System.out.println("  Doubly Linked List Element :");
            // Get first node of linked list
            LinkNode temp = this.head;
            // iterate linked list 
            while (temp != null)
                // Display node value
                System.out.print("  " +;
                // Visit to next node
                temp =;
    public static void main(String[] args)
        DoublyLinkedList dll = new DoublyLinkedList();
        // Insert following linked list nodes
        //  NULL <- 10 <--> 20 <--> 30 <--> 40 <--> 50 <--> 60 <--> 70 -> NULL

This is a Java code for implementing a Doubly Linked List data structure. Here's a brief explanation of the code:

  1. The LinkNode class defines the structure of a doubly linked list node. It has three instance variables: data to store the data, next to store the reference to the next node, and prev to store the reference to the previous node.

  2. The DoublyLinkedList class implements the doubly linked list. It has a single instance variable head which stores the reference to the first node of the linked list.

  3. The insert method is used to insert a new node at the beginning of the linked list. It creates a new node with the given value and sets its next reference to the current head. If the linked list is not empty, it sets the prev reference of the current head to the new node. Finally, it sets the head reference to the new node.

  4. The display method is used to display the elements of the doubly linked list. It iterates through the linked list starting from the head and prints the value of each node.

  5. The main method creates an instance of the DoublyLinkedList class and inserts some nodes into it. Then, it calls the display method to print the elements of the linked list.

Insert node in front of doubly linked list node


  Doubly Linked List Element :
  10  20  30  40  50  60  70


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment