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.

insert at beginning in 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.

insertion at beginning in a doubly linked list

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:

Each includes algorithm, Java code, sample I/O, and complexities.

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

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

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Doubly Linked List

  • Introduction to Doubly Linked list in Data Structure
    Click Here
  • Doubly Linked List in –
    C | C++ | Java
  • Insertion in doubly linked list –
    C | C++ | Java
  • Insertion at beginning in doubly linked list –
    C | C++ | Java
  • Insertion at end in doubly linked list –
    C | C++ | Java
  • Insertion at nth node in doubly linked list –
    C | C++ | Java
  • Deletion in doubly linked list  –
    C | C++ | Java
  • Deletion from beginning in doubly linked list :
  • Deletion from nth in doubly linked list :
    C | C++ | Java
  • Deletion from end in doubly linked list :
    C | C++ | Java
  • Insertion and Deletion in a  doubly linked list :
    C | C++ | Java
  • Insertion in the middle in a  doubly linked list :
    C | C++ | Java

Doubly Linked List