JAVA Program for Insertion at the Beginning of a Doubly Linked List
Insertion at the Beginning of a Doubly Linked List in Java
In this article you will learn the complete logic behind Java Program for Insertion at the Beginning of a Doubly Linked List, along with algorithms, examples, and clean Java implementations.
Insertion at the beginning of a doubly linked list is one of the most common operations in data structures. This topic is important because it demonstrates how pointer manipulation works in both directions in a doubly linked list.
Java Program for Insertion at the Beginning of a Doubly Linked List
What Does Insertion at the Beginning Mean?
Doubly linked list contains nodes where each node has:
- data
- prev pointer (points to previous node)
- next pointer (points to next node)
Insertion at the beginning means creating a new node and placing it before the current head, making it the new head of the list.
Example:
Before insertion:
20 ⇆ 30 ⇆ 40
Insert 10 at beginning →
10 ⇆ 20 ⇆ 30 ⇆ 40
Both prev and next links must be updated properly.
Understanding Java Program for Insertion at the Beginning of a Doubly Linked List helps you learn:
- How forward and backward pointers work
- Creating new nodes and reassigning references
- Handling empty list situations
- Preparing for coding interviews
This operation is foundational for building editors, browsers, playlist management, and many real-world linked structures.
1. Create a new node.
2. Set newNode.next = head.
3. If list is not empty, set head.prev = newNode.
4. Set newNode as the new head.
5. Return updated head.
Methods for Insertion at the Beginning of a Doubly Linked List in Java
Here we have basically 2 approaches for Insertion at the Beginning of a Doubly Linked List:
- Method 1: Basic Pointer Manipulation (Iterative)
- Method 2: Using a Doubly Linked List Class Structure
Each includes algorithm, Java code, sample I/O, and complexities.
Learn DSA
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Algorithm to write a function to add a Node in the Beginning of a Linked List
- AppendStart(int data)
- Node newNode = new Node(data)
- IF HEAD == NULL
- newNode HEAD = TAIL = newNode
- HEAD.previous = NULL
- TAIL.next = NULL
- ELSE
- HEAD.previous = newNode
- newNode.next = HEAD
- newNode.previous = NULL
- HEAD = newNode
JAVA Program to Insert a Node at the Beginning of a Linked List
import java.lang.*;
class DoublyLinkedList
{
Node head;
// not using parameterized constructor would by default
// force head instance to become null
// Node head = null; // can also do this, but not required
// Node Class
class Node
{
int data;
Node next, prev;
Node (int x) // parameterized constructor
{
data = x;
next = null;
prev = null;
}
}
public void insertBeginning (int data)
{
// Creating newNode memory & assigning data value
Node freshNode = new Node (data);
freshNode.next = head;
freshNode.prev = null;
// if DLL had already >=1 nodes
if (head != null)
head.prev = freshNode;
// changing head to this
head = freshNode;
}
public void printList ()
{
Node node = head;
Node end = null;
//as linked list will end when Node reaches Null
System.out.print ("\nIn forward: ");
while (node != null)
{
System.out.print (node.data + " ");
end = node;
node = node.next;
}
System.out.print ("\nIn backward: ");
while (end != null)
{
System.out.print (end.data + " ");
end = end.prev;
}
System.out.println ();
}
}
class Main
{
public static void main (String args[])
{
DoublyLinkedList doublylist = new DoublyLinkedList ();
doublylist.insertBeginning (3);
doublylist.insertBeginning (2);
doublylist.insertBeginning (1);
doublylist.insertBeginning (4);
doublylist.insertBeginning (5);
doublylist.printList ();
}
}
Output
In forward: 5 4 1 2 3 In backward: 3 2 1 4 5
import java.lang.*;
// Node Class
class Node
{
int data;
Node next, prev;
Node (int x) // parameterized constructor
{
data = x;
next = null;
prev = null;
}
}
class Main
{
static Node insertBeginning (Node head, int data)
{
// Creating newNode memory & assigning data value
Node newNode = new Node (data);
newNode.next = head;
newNode.prev = null;
// if DLL had already >=1 nodes
if (head != null)
head.prev = newNode;
// changing head to this
head = newNode;
return head;
}
static void printList (Node temp)
{
Node end = null;
//as linked list will end when Node reaches Null
System.out.print ("\nIn forward: ");
while (temp != null)
{
System.out.print (temp.data + " ");
end = temp;
temp = temp.next;
}
System.out.print ("\nIn backward: ");
while (end != null)
{
System.out.print (end.data + " ");
end = end.prev;
}
System.out.println ();
}
// required for insertAfterPosition() method
static int getLength (Node node)
{
int size = 0;
// traverse to the last node each time incrementing the size
while (node != null)
{
node = node.next;
size++;
}
return size;
}
public static void main (String args[])
{
Node head = null;
head = insertBeginning (head, 3);
head = insertBeginning (head, 2);
head = insertBeginning (head, 1);
head = insertBeginning (head, 4);
head = insertBeginning (head, 5);
printList (head);
}
}
Output
In forward: 5 4 1 2 3 In backward: 3 2 1 4 5
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Doubly Linked List
- Introduction to Doubly Linked list in Data Structure
Click Here - Doubly Linked List in –
- Insertion in doubly linked list –
- Insertion at beginning in doubly linked list –
- Insertion at end in doubly linked list –
- Insertion at nth node in doubly linked list –
- Deletion in doubly linked list –
- Deletion from beginning in doubly linked list :
- Deletion from nth in doubly linked list :
- Deletion from end in doubly linked list :
- Insertion and Deletion in a doubly linked list :
- Insertion in the middle in a doubly linked list :
Doubly Linked List
- Introduction to Doubly Linked list in Data Structure
- Doubly Linked List in – C | C++ | Java
- Insertion in doubly linked list – C | C++ | Java
- Deletion in doubly linked list – C | C++ | Java
- Insertion and Deletion in doubly linked list – C | C++ | Java
- Insertion in the middle in a doubly linked list – C | C++ | Java
