# JAVA Program for Insertion at the Nth Position of a Doubly Linked List

## Java Program for Insertion at the Nth Position in a Doubly Linked List

In this page we’ll take a in-depth look on the process of Insertion a Node at the desired location in a Doubly Linked List along with a Program in JAVA Language to perform the similar Operation. The Process of insertion in a doubly linked list is somewhat similar to the process of insertion in  a Singly Linked List, the difference here is just that here we have a extra pointer (Previous) that needs to be directed to the address of the node lying before the node that is being Inserted.

## Steps to be followed while Inserting a Node at a Specific location 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 at the Nth Position of the Doubly Linked List, we’ll have to store and redirect various links of the Linked List.
• First of all the address stored in the next pointer of  the (n-1)th node of the List will now store the address of the New Node that is being inserted.
• Now the address stored in the Previous Pointer of the (n+1)th node of the Linked List will also be re-directed to the address of the New Node being inserted.
• Now, at last the Previous Pointer of the New Node will be directed towards the address of the node at (n-1)th position and  the Next Pointer of the New Node will be directed towards the address of the node at (n+1)th position.

## Algorithm to insert a node at the specific position in a doubly linked list

• Node node=new Node()
• node.data=data
• node.nextNode=null
• if(location!=0)
• return
• else
• return
• Node prev =Nnull
• while(i=0)
• tempNode=tempNode.nextNode

## Java program to Insert a Node at the Specific Position in a Doubly Linked List

```import java.util.*;

public class PrepInsta
{

class Node
{
int data;
Node nextNode;

}

public void append (int data)
{
//make a new Node and store a data.
Node node = new Node ();
node.data = data;
node.nextNode = null;

//check if the head is null or not.
//if head is null, assign the Node and exit.
{

return;
}

//define a head into the temp Node and loop it until find the null.
while (tempNode.nextNode != null)
{
tempNode = tempNode.nextNode;
}

tempNode.nextNode = node;
}

public void AppendNthNode (int data, int location)
{

//create new node.
Node node = new Node ();
node.data = data;
node.nextNode = null;

{

//if head is null and location is zero then exit.
if (location != 0)
{

return;
}
else
{

// set node to the head.
}
}

if (head != null && location == 0)
{
return;
}

Node prev = null;

int i = 0;

while (i ");
tempNode = tempNode.nextNode;
}
System.out.println ("NULL");
}

public static void main (String[]args)
{
PrepInsta dlist = new PrepInsta ();
dlist.append(1);
dlist.append(2);
dlist.append(4);
dlist.append(5);
dlist.AppendNthNode(3, 2);
dlist.print();
}
}
```