JAVA Program for Insertion at the Beginning of a Doubly Linked List

Java Program for Insertion at beginning

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.

insert at beginning in doubly linked list

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.

Example :

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 ) .

insertion at beginning in a doubly linked list

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

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

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