# Java Program for insertion at the beginning in doubly linked list

Adding or inserting a node in the beginning in doubly linked list is almost similar as the process of adding a node in singly linked list . The only difference is that we have an extra pointer (previous node) to be redirected.We will create a doubly linked list and insert every new node at the beginning of the list.

Example of Insertion in the Beginning of a Doubly Linked List-

If we have doubly linked listed like, (1–>2–>3–>4) and we have to add 5 at the beginning of the list then after adding linked list would look like  (5–>1–>2–>3–>4 ) .

## Steps to be followed while Inserting a Node at the Beginning of a Doubly Linked List

• Check for the presence of Node in the List, if there exists some Nodes, Continue.
• Now, to insert a node in the beginning of the Doubly Linked List, we’ll have to store and redirect various links of the Linked List.
• First of all the Head will now store the address of the space where the data of the New Node is stored.
• Now Since the New Node is going to be the first Node of the Linked List. So, the Previous Pointer of the New Node will point to Null.
• Then the Next Pointer of the New Node will now point to the Previously First Node of the List.
• Now, at last the Previous Pointer of the Previously First node of the list will be directed towards the New Node that is being Inserted.

## Algorithm to write a function to add a Node in the Beginning of a Linked List

• AppendStart(int data)
• Node newNode = new Node(data)
• newNode HEAD = TAIL = newNode
• TAIL.next = NULL
• ELSE
• newNode.previous = NULL

## JAVA Program to Insert a Node at the Beginning of a Linked List

```     import java.util.*;

class PrepInsta
{

//Constitute a node of the doubly linked list

class Node{
int data;
Node previous;
Node next;

public Node(int data) {
this.data = data;
}
}

// Function to traverse and print the linked list
public void display() {
while (temp != null) {
System.out.print(temp.data + "  ");
// Set temp to point to the next node
temp = temp.next;
}
System.out.println("END");
}

//Function to add a node to the starting of the list
public void AppendStart(int data) {
//Create a new Node
Node newNode = new Node(data);

//If list is empty
//head and tail both will point to newNode
//head's previous will point to null
//tail's next will point to null, because it is the last node of the list
tail.next = null;
}
//make newNode as new head of the list
else {
//head's previous pointer node will be newNode
//newNode's next pointer node will be head
//newNode's pointer previous will point to null
newNode.previous = null;
}
}

//print() will print the nodes of the list
public void print() {
//current Node will point to head
System.out.println("List is empty");
return;
}
System.out.println("Append a node to the start of the list: ");
while(curr != null) {
//Prints each node by incrementing the pointer.

System.out.print(curr.data + " ");
curr = curr.next;
}
System.out.println();
}

public static void main(String[] args) {

PrepInsta dList = new PrepInsta();

//Appending 5 to the list
dList.AppendStart(5);
dList.print();
//Appending 4 to the list
dList.AppendStart(4);
dList.print();
//Appending 3 to the list
dList.AppendStart(3);
dList.print();
//Appending 2 to the list
dList.AppendStart(2);
dList.print();
//Appending 1 to the list
dList.AppendStart(1);
dList.print();
}
}```

## Output:

```Append a node to the start of the list:
5
Append a node to the start of the list:
4 5
Append a node to the start of the list:
3 4 5
Append a node to the start of the list:
2 3 4 5