Insertion at the End in Circular Linked List in Java

Java Program for Insertion at the end in Circular Linked List

Insertion at the end in Circular Linked List in Java is one of the most common operations performed on linked lists. It allows us to add a new node after the last node, while maintaining the circular structure of the list.

Understanding this concept is essential for beginners learning Data Structures and Algorithms in Java, especially when working with dynamic data where the number of elements is not fixed.

Insertion-at-the-End-in-Circular-Linked-List-in-Java

Insertion at the end in Circular Linked List in Java

Understanding Insertion at the End in Circular Linked List

Insertion at the end means adding a new node after the last node and then updating the last node’s next pointer to point to the head, maintaining the circular structure.

Insertion at the end in a circular singly linked list means adding a new node after the current last node, while maintaining the circular structure.

After insertion:

  1. The new node becomes the new last node.
  2. The next of the new node points to the head.
  3. The next of the previous last node points to the new node.

Node Structure:

class Node {
    int data;
    Node next;

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

To implement and perform operations (like insertion, deletion, or traversal) efficiently, we typically maintain:

  • head → Reference to the first node in the list.
  • tail → Reference to the last node in the list.

In a circular linked list, tail.next always points to head.

Insertion in Circular Linked List

Methods for Insertion at the End in Circular Linked List in Java

There are multiple ways to insert a node at the end in a circular linked list:

Method 1: Traversing from the Head until the Last Node

Method 2: Using a Tail Pointer

Methods for Insertion at the End in Circular Linked List in Java

Method 1: Using Traversal from Head

Algorithm for Insertion at the End:

  1. Create a new node with the given data.

  2. If the list is empty, make the new node point to itself and mark it as the head.

  3. Otherwise, start from the head and traverse until you find the last node (whose next is head).

  4. Set the next of the last node to the new node.

  5. Set the new node’s next pointer to the head.

  6. The circular structure is maintained.

Java Code:

Run
// Insertion at End in Circular Linked List using Traversal
class CircularLinkedList {
    Node head;

    // Node structure
    class Node {
        int data;
        Node next;

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

    // Function to insert node at end
    public void insertAtEnd(int data) {
        Node newNode = new Node(data);

        // Case 1: If list is empty
        if (head == null) {
            head = newNode;
            newNode.next = head; // Point to itself
            return;
        }

        // Case 2: Traverse to last node
        Node temp = head;
        while (temp.next != head) {
            temp = temp.next;
        }

        // Insert at end
        temp.next = newNode;
        newNode.next = head;
    }

    // Display the circular linked list
    public void display() {
        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();
    }

    // Main function to test
    public static void main(String[] args) {
        CircularLinkedList cll = new CircularLinkedList();
        cll.insertAtEnd(10);
        cll.insertAtEnd(20);
        cll.insertAtEnd(30);

        System.out.println("Circular Linked List after insertion:");
        cll.display();
    }
}

Output:

Circular Linked List after insertion:
10 20 30

Methods for Insertion at the End in Circular Linked List

Method 2: Using a Tail Pointer (Optimized Method)

Algorithm for Insertion at the End:

To avoid traversing the entire list every time, we maintain a tail pointer that always points to the last node.

This allows insertion at the end in O(1) time.

  1. Maintain a tail pointer that points to the last node.

  2. Create a new node with the given data.

  3. If the list is empty:

    • Make the new node point to itself.

    • Set both head and tail to the new node.

  4. Otherwise:
    • Set the new node’s next pointer to head.

    • Set tail’s next pointer to the new node.

    • Update tail to point to the new node.

Java Code:

Run
// Insertion at End in Circular Linked List using Tail Pointer
class CircularLinkedListOptimized {
    Node head, tail;

    // Node structure
    class Node {
        int data;
        Node next;

        Node(int data) {
            this.data = data;
        }
    }

    // Function to insert at end using tail pointer
    public void insertAtEnd(int data) {
        Node newNode = new Node(data);

        // Case 1: Empty list
        if (head == null) {
            head = newNode;
            tail = newNode;
            newNode.next = head;
            return;
        }

        // Case 2: Insert directly using tail
        newNode.next = head;
        tail.next = newNode;
        tail = newNode;
    }

    // Display the circular linked list
    public void display() {
        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();
    }

    // Main function to test
    public static void main(String[] args) {
        CircularLinkedListOptimized cll = new CircularLinkedListOptimized();
        cll.insertAtEnd(5);
        cll.insertAtEnd(15);
        cll.insertAtEnd(25);
        cll.insertAtEnd(35);

        System.out.println("Circular Linked List after optimized insertion:");
        cll.display();
    }
}

Output:

Circular Linked List after optimized insertion:
5 15 25 35

Comparison between Methods

CriteriaMethod 1: TraversalMethod 2: Tail Pointer
ApproachTraverse from head till last nodeMaintain direct pointer to last node
Time ComplexityO(n)O(1)
Space ComplexityO(1)O(1)
Ease of ImplementationSimpleSlightly advanced
Suitable ForSmall listsFrequent insertions at end

Insertion at the end in a Circular Linked List in Java is a foundational concept in data structures that helps build efficient and flexible programs.

While the traversal method is simpler and suitable for small lists, the tail pointer method offers better efficiency for frequent insertions. Understanding both approaches ensures you can choose the right method based on the application’s needs.

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

Answer:

It is the process of adding a new node after the last node while maintaining the circular connection where the last node points to the head.

Answer:

In a singly linked list, the last node’s next pointer is null, while in a circular linked list it points back to the head node.

Answer:

The tail pointer method is faster as it performs insertion in O(1) time compared to O(n) in traversal.

Answer:

While possible, recursion is not efficient or common for circular lists because it adds extra stack space and complexity.

Answer:

Yes, by setting the last node’s next pointer to the head node, a singly linked list can be converted into a circular 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