Open In App

Java Program to Insert a New Node at the Beginning of the Circular Linked List

Last Updated : 15 Aug, 2022
Like Article

Circular linked list: A circular linked list is a sequence of elements in which every element points to its next element in the sequence and the last element has a link to the first element. That means a circular linked list is similar to the single linked list except that the last node points to the first node in the list.

Inserting a new node at the beginning of the circular linked list:

If the linked list is empty then both head and tail will point to the newly added node. If the list is not empty, then we will point the last node to newly added node and point the newly added node to head node and finally, we make the newly added node as the head node.

Add Node 0 at the beginningAdded Node 0 at the beginning

Algorithm : 

  1. Create a Node class which represents a node in the list. It has two variables data and next pointer(which points to the next node).
  2. Create another class for creating the circular linked list and it has two nodes namely head and tail.
  3. When adding a new node to list then, we will first check whether the head is null. If the list is empty or head is null then we will insert the node as the head, and tail also points to the newly added node.
  4. If the list is not empty, then the newly added node will point to head, and the tail will point to a newly added node and New node will be made as to the head node.

Code Snippet :


// Java Program to Insert a nodes at the Beginning of the
// Circular Linked List
public class AddAtBeginning {
    // Represents the node of list.
    public class Node {
        char data;
        Node next;
        public Node(char data) { = data; }
    // Declaring head and tail pointer as null.
    // head indicates the starting node and tail indicates
    // last node.
    public Node head = null;
    public Node tail = null;
    // This function will add the new node at the Beginning
    // of the circular linked list.
    public void addNode(char data)
        // Create new node
        Node newNode = new Node(data);
        // Checks if the list is empty.
        if (head == null) {
            // make newnode as both head and tail node.
            head = newNode;
            tail = newNode;
            // point the tail to head node.
   = head;
        else {
            // point the newnode to head
   = head;
            // point the rail to new node.
   = newNode;
            // make the newnode as head.
            head = newNode;
    // printLinkedList prints all the nodes in the list
    public void printLinkedList()
        Node presentNode = head;
        if (head == null) {
            System.out.println("List is empty");
        else {
            // here with out checking anything we will print
            // the first node, And print the rest of the
            // nodes till the pointer again reaches the head
            // node.
            do {
                System.out.print(" " +;
                // incrementing the nodes using next
                // property.
                presentNode =;
                // if present node is head, stop printing
                // the nodes.
            } while (presentNode != head);
    public static void main(String[] args)
        AddAtBeginning obj = new AddAtBeginning();
            "Adding nodes at the beginning of the list: ");
        // add k at the beginning
        // add e at the beginning
        // add e at the beginning
        // add G at the beginning


Adding nodes at the beginning of the list: 


 k s

 e k s

 e e k s

 G e e k s

Time Complexity: O(1)

Auxiliary space: O(1) as it is using constant space

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads