Insertion at the Beginning in Circular Linked List in Java

Java Program for Insertion at the Beginning in Circular Linked List

Insertion at the Beginning in Circular Linked List in Java is a common operation when working with circular (singly) linked lists.

This article explains what that operation means, multiple ways to implement it, the algorithmic logic with step by step variable level explanations, runnable Java code, example input/output, time and space complexity.

Insertion at the beginning in circular linked list in java

Insertion at the Beginning in Circular Linked List​ in Java

Circular singly linked list is a linked list where every node points to the next node, and the last node points back to the first node (head), forming a circle. There is no null next pointer in a non empty circular list.

  • Keep a pointer/reference to the head (first element).
  • Or keep a pointer/reference to the tail (last element). If you have tail, then tail.next is the head.

Insertion at the beginning means that after the operation, the new node must be the first node (head) of the circular list, and the circular property must still hold.

Node Structure:

class Node {
    int data;
    Node next;
    Node(int data) { this.data = data; this.next = null; }
}

Variables used in algorithms:

  • head: reference to the first node (may be null for empty list)

  • tail: reference to last node (if used)

  • newNode: the node created to insert

  • temp / last: iteration pointers for traversing

Methods for Insertion at the Beginning in Circular Linked List​

  1. Traverse to last node (no tail pointer)
  2. Use a tail pointer
  3. Data swap trick

Now, we will study each method with algorithms, step by step logic, and complexity.

Insertion in beginning of Circular Linked List in java

Methods for Insertion at the Beginning in Circular Linked List​ in Java

Method 1: Using Head Pointer Only (Traversal Approach)

Algorithm for Insertion at the beginning:

  1. Create a new node newNode.
  2. If the list is empty:
    • Point newNode.next to itself.
    • Set head = newNode.
  3. Otherwise:
    • Traverse till last.next == head.
    • Set newNode.next = head.
    • Set last.next = newNode.
    • Update head = newNode.

Java Code:

Run
public class CircularListInsertionTraverse {
    static class Node {
        int data;
        Node next;
        Node(int data) { this.data = data; }
    }

    Node head = null;

    public void insertAtBeginning(int value) {
        Node newNode = new Node(value);
        if (head == null) {
            newNode.next = newNode;
            head = newNode;
            return;
        }

        Node temp = head;
        while (temp.next != head)
            temp = temp.next;

        newNode.next = head;
        temp.next = newNode;
        head = newNode;
    }

    public void printList() {
        if (head == null) {
            System.out.println("List is empty.");
            return;
        }
        Node temp = head;
        do {
            System.out.print(temp.data + " -> ");
            temp = temp.next;
        } while (temp != head);
        System.out.println("(back to head)");
    }

    public static void main(String[] args) {
        CircularListInsertionTraverse list = new CircularListInsertionTraverse();
        list.insertAtBeginning(30);
        list.insertAtBeginning(20);
        list.insertAtBeginning(10);

        System.out.println("Circular Linked List after insertions:");
        list.printList();
    }
}

Output:

Circular Linked List after insertions:
10 -> 20 -> 30 -> (back to head)

Method 2: Using Tail Pointer

Algorithm for Insertion at the beginning:

  1. Create a new node newNode.
  2. If the list is empty:
    • newNode.next = newNode.

    • tail = newNode.

  3. Otherwise:
    • newNode.next = tail.next (current head).

    • tail.next = newNode.

  4. The new head is tail.next.

Java Code:

Run
public class CircularListInsertionTail {
    static class Node {
        int data;
        Node next;
        Node(int data) { this.data = data; }
    }

    Node tail = null;

    public void insertAtBeginning(int value) {
        Node newNode = new Node(value);
        if (tail == null) {
            tail = newNode;
            tail.next = tail;
            return;
        }

        newNode.next = tail.next; // current head
        tail.next = newNode;      // link tail to new node
    }

    public void printList() {
        if (tail == null) {
            System.out.println("List is empty.");
            return;
        }
        Node temp = tail.next; // start from head
        do {
            System.out.print(temp.data + " -> ");
            temp = temp.next;
        } while (temp != tail.next);
        System.out.println("(back to head)");
    }

    public static void main(String[] args) {
        CircularListInsertionTail list = new CircularListInsertionTail();
        list.insertAtBeginning(30);
        list.insertAtBeginning(20);
        list.insertAtBeginning(10);

        System.out.println("Circular Linked List after insertions:");
        list.printList();
    }
}

Output:

Circular Linked List after insertions:
10 -> 20 -> 30 -> (back to head)

Method 3: Data Swap Technique

Algorithm for Insertion at the beginning:

  1. Create a new node newNode.
  2. If the list is empty:
    • newNode.next = newNode

    • head = newNode

  3. Otherwise:
    • Insert newNode after head.

    • Swap head.data and newNode.data.

Java Code:

Run
public class CircularListInsertionDataSwap {
    static class Node {
        int data;
        Node next;
        Node(int data) { this.data = data; }
    }

    Node head = null;

    public void insertAtBeginning(int value) {
        Node newNode = new Node(value);
        if (head == null) {
            head = newNode;
            head.next = head;
            return;
        }

        newNode.next = head.next;
        head.next = newNode;

        // Swap data between head and new node
        int temp = head.data;
        head.data = newNode.data;
        newNode.data = temp;
    }

    public void printList() {
        if (head == null) {
            System.out.println("List is empty.");
            return;
        }
        Node temp = head;
        do {
            System.out.print(temp.data + " -> ");
            temp = temp.next;
        } while (temp != head);
        System.out.println("(back to head)");
    }

    public static void main(String[] args) {
        CircularListInsertionDataSwap list = new CircularListInsertionDataSwap();
        list.insertAtBeginning(30);
        list.insertAtBeginning(20);
        list.insertAtBeginning(10);

        System.out.println("Circular Linked List after insertions:");
        list.printList();
    }
}

Output:

Circular Linked List after insertions:
10 -> 20 -> 30 -> (back to head)

Comparison between Methods for Insertion at the Beginning in Circular Linked List

MethodUses Tail?Time ComplexitySpace ComplexityNotes
Head Only (Traverse)NoO(n)O(1)Simple but slower
Tail PointerYesO(1)O(1)Best for efficiency
Data Swap TrickNoO(1)O(1)Fast but swaps data

Insertion at the beginning of a circular linked list in Java can be implemented in multiple ways depending on what references you maintain (head, tail) and whether you can modify node data.

  1. Use a tail pointer for clean O(1) operations.
  2. use the data swap trick if you can’t maintain tail and swapping data is acceptable.
  3. otherwise fall back to a traversal based correct O(n) insertion.

The included Java code demonstrates all methods and is ready to run and test.

FAQ's Related to Insertion at the beginning in circular linked list

Answer:

It means adding a new node before the current head so that it becomes the new first node while maintaining the circular connection.

Answer:

By maintaining a tail pointer simply link the new node between tail and tail.next.

Answer:

Yes, but it requires O(n) time (traversing to the last node) or using the data swap trick.

Answer:

To form a continuous loop, ensuring traversal from any node eventually brings you back to the starting node.

Answer:

Maintain both head and tail. It simplifies operations like insertion and deletion while keeping O(1) performance.

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