Insertion in the Beginning of a Circular Linked List in JAVA

Longest subsequence between two strings

JAVA Program to Insert a Node in the Beginning of a Circular Linked List

Insertion in any type of a Linked List is just modification of the address stored in the references of the Nodes, Insertion in the Beginning of a Circular Linked List in JAVA is also achieved using similar minor modification. For insertion in the beginning the address stored in the HEAD now points to the new node and the New Node will now point towards the Node where head was earlier pointing to.

Steps to insert a node in the Beginning of a Circular Linked List

While Insertion in the Beginning of a Circular Linked List in JAVA two cases can be considered.

  • The circular linked is empty –
    • To insert an element in an empty circular list is quite easy.
    • Both the head and the tail points to the very first element in a circular linked list.
  • The circular linked list is not empty –
    • In a circular linked list the first element serves the purpose of a head. It stores the information of the next node of the circular linked.
    • To insert an element in the beginning of the linked list. The new node should point towards the previous Head making it the new head of the circular linked list.
    • The tail is the last node that points back to the first note making the linked list circular because the traversal can be done from start till end.
    • To add a node in the beginning means to change the node tail is referring to newly added node. By doing this the new node will be inserted in the beginning of the circular linked list.
Insertion in beginning of Circular Linked List in java
public void insertBegin(int element){
        Node newEle = new Node(element);
        if(head == null) {
            head = newEle;
            tail = newEle;
            newEle.next = head;
            newEle.prev=head;
        }
        else {
            head.prev = newEle;
            newEle.prev=tail;
            newEle.next = head;
            tail.next = newEle;
            head=newEle;
        }
        size++;
    }

Here is the Algorithm to add a Node at the Beginning of a Linked List

  • addFirst(element)
  • IF Head==Null
    • Head -> newNode
    • Tail -> newNode
    • newNode->next = Head
    • EXIT
  • ELSE
    • Node Temp = head 
    • newNode->next = Temp
    • head = newNode
    • tail->next = Head
    • EXIT

Java code for insertion at beginning in a circular 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

Circular Linked List

  • Introduction to Circular Linked List
    Click Here
  • Circular Linked List Applications
    Click Here
  • Circular Linked List in –
    C | C++ | Java
  • Insertion in Circular Linked List –
    C | C++ | Java
  • Insertion at the beginning–
    C | C++ | Java
  • Insertion at the end –
    C | C++ | Java
  • Insertion at nth position –
    C | C++ | Java
  • Deletion in Circular Linked List –
    C | C++ | Java
  • Deletion from beginning in Circular Linked List –
    C | C++ | Java
  • Deletion from nth position in Circular Linked List –
  • Deletion from end in Circular Linked List –
    C | C++ | Java
  • Insertion and Deletion in Circular Linked List – C | C++ | Java
  • Split a Circular Linked List in two halves –
    C | C++ | Java
  • Count nodes in Circular Linked List –
    C | C++ | Java
  • Sorted Insert In Circular Linked List –
    C | C++ | Java
  • Insertion in the middle in Circular Linked List –
    C | C++ | Java

Circular Linked List