Insertion at the end in circular linked list

Last Updated : 1 Jul, 2026

Given the tail of a circular linked list and an integer key, insert a new node with the given key at the end of the circular linked list.

Examples:

Input: key = 5,

8

Output: 1 -> 7 -> 8 -> 10 -> 5
Explanation: After inserting 5 at the end of the given circular linked list, it has elements as 1, 7, 8, 10, 5.

2056958421


Input: key = 1,

7

Output: 2 -> 5 -> 7 -> 8 -> 10 -> 1
Explanation: After inserting 1 at the end of the given circular linked list, it has elements as 2, 5, 7, 8, 10, 1.

409843031
Try It Yourself
redirect icon

Insertion at the End in Circular Linked List - O(1) Time and O(1) Space

The idea is to create a new node and insert it immediately after the current tail of the circular linked list. If the list is empty, make the new node point to itself and return it as the tail. Otherwise, link the new node to the current head (tail->next), update the current tail's next pointer to the new node, and return the new node as the updated tail. This inserts the node at the end while maintaining the circular nature of the linked list.

Step-by-step approach:

  • Create a new node with the given key.
  • If the circular linked list is empty (tail == nullptr), make newNode->next point to itself and return newNode as the tail.
  • Otherwise, set newNode->next to tail->next (the current head).
  • Update tail->next to newNode.
  • Return newNode as the new tail.
C++
#include <bits/stdc++.h>
using namespace std;

class Node
{
  public:
    int data;
    Node *next;

    Node(int x)
    {
        data = x;
        next = nullptr;
    }
};

Node *insertAtEnd(Node *tail, int key)
{

    // Create a new node with the given key.
    Node *newNode = new Node(key);

    // If the circular linked list is empty,
    // make the new node point to itself.
    if (tail == nullptr)
    {
        newNode->next = newNode;
        return newNode;
    }

    // Insert the new node after the tail
    // and before the current head.
    newNode->next = tail->next;
    tail->next = newNode;

    // The newly inserted node becomes the new tail.
    return newNode;
}

// Function to print the circular linked list
void printList(Node *tail)
{
    if (tail == nullptr)
    {
        cout << "Empty";
        return;
    }

    Node *curr = tail->next;

    do
    {
        cout << curr->data;
        curr = curr->next;

        if (curr != tail->next)
            cout << "->";

    } while (curr != tail->next);
}

int main()
{

    // Create the circular linked list: 2 -> 5 -> 7 -> 8 -> 10
    Node *tail = new Node(10);
    tail->next = new Node(2);
    tail->next->next = new Node(5);
    tail->next->next->next = new Node(7);
    tail->next->next->next->next = new Node(8);
    tail->next->next->next->next->next = tail;

    int key = 1;

    tail = insertAtEnd(tail, key);

    printList(tail);

    return 0;
}
Java
import java.util.*;

class Node {
    public int data;
    public Node next;

    public Node(int x)
    {
        data = x;
        next = null;
    }
}

public class GFG {
    public static Node insertAtEnd(Node tail, int key)
    {

        // Create a new node with the given key.
        Node newNode = new Node(key);

        // If the circular linked list is empty,
        // make the new node point to itself.
        if (tail == null) {
            newNode.next = newNode;
            return newNode;
        }

        // Insert the new node after the tail
        // and before the current head.
        newNode.next = tail.next;
        tail.next = newNode;

        // The newly inserted node becomes the new tail.
        return newNode;
    }

    // Function to print the circular linked list
    public static void printList(Node tail)
    {
        if (tail == null) {
            System.out.print("Empty");
            return;
        }

        Node curr = tail.next;

        do {
            System.out.print(curr.data);
            curr = curr.next;

            if (curr != tail.next)
                System.out.print("->");

        } while (curr != tail.next);
    }

    public static void main(String[] args)
    {

        // Create the circular linked list: 2 -> 5 -> 7 -> 8
        // -> 10
        Node tail = new Node(10);
        tail.next = new Node(2);
        tail.next.next = new Node(5);
        tail.next.next.next = new Node(7);
        tail.next.next.next.next = new Node(8);
        tail.next.next.next.next.next = tail;

        int key = 1;

        tail = insertAtEnd(tail, key);

        printList(tail);
    }
}
Python
class Node:
    def __init__(self, data):
        self.data = data
        self.next = None


def insertAtEnd(tail, key):
    # Create a new node with the given key.
    newNode = Node(key)

    # If the circular linked list is empty,
    # make the new node point to itself.
    if tail is None:
        newNode.next = newNode
        return newNode

    # Insert the new node after the tail
    # and before the current head.
    newNode.next = tail.next
    tail.next = newNode

    # The newly inserted node becomes the new tail.
    return newNode

# Function to print the circular linked list


def printList(tail):
    if tail is None:
        print('Empty')
        return

    current = tail.next

    while True:
        print(current.data, end='')
        current = current.next
        if current != tail.next:
            print('->', end='')
        if current == tail.next:
            break


# Create the circular linked list: 2 -> 5 -> 7 -> 8 -> 10
tail = Node(10)
tail.next = Node(2)
tail.next.next = Node(5)
tail.next.next.next = Node(7)
tail.next.next.next.next = Node(8)
tail.next.next.next.next.next = tail

key = 1
tail = insertAtEnd(tail, key)

printList(tail)
C#
using System;

public class Node {
    public int data;
    public Node next;

    public Node(int x)
    {
        data = x;
        next = null;
    }
}

public class GFG {
    public static Node insertAtEnd(Node tail, int key)
    {
        // Create a new node with the given key.
        Node newNode = new Node(key);

        // If the circular linked list is empty,
        // make the new node point to itself.
        if (tail == null) {
            newNode.next = newNode;
            return newNode;
        }

        // Insert the new node after the tail
        // and before the current head.
        newNode.next = tail.next;
        tail.next = newNode;

        // The newly inserted node becomes the new tail.
        return newNode;
    }

    // Function to print the circular linked list
    public static void printList(Node tail)
    {
        if (tail == null) {
            Console.Write("Empty");
            return;
        }

        Node curr = tail.next;

        do {
            Console.Write(curr.data);
            curr = curr.next;

            if (curr != tail.next)
                Console.Write("->");

        } while (curr != tail.next);
    }

    public static void Main()
    {
        // Create the circular linked list: 2 -> 5 -> 7 -> 8
        // -> 10
        Node tail = new Node(10);
        tail.next = new Node(2);
        tail.next.next = new Node(5);
        tail.next.next.next = new Node(7);
        tail.next.next.next.next = new Node(8);
        tail.next.next.next.next.next = tail;

        int key = 1;

        tail = insertAtEnd(tail, key);

        printList(tail);
    }
}
JavaScript
class Node {
    constructor(data)
    {
        this.data = data;
        this.next = null;
    }
}

function insertAtEnd(tail, key)
{
    // Create a new node with the given key.
    const newNode = new Node(key);

    // If the circular linked list is empty,
    // make the new node point to itself.
    if (tail === null) {
        newNode.next = newNode;
        return newNode;
    }

    // Insert the new node after the tail
    // and before the current head.
    newNode.next = tail.next;
    tail.next = newNode;

    // The newly inserted node becomes the new tail.
    return newNode;
}

function printList(tail)
{
    if (tail === null) {
        console.log("Empty");
        return;
    }

    let curr = tail.next;

    do {
        process.stdout.write(curr.data.toString());
        curr = curr.next;

        if (curr !== tail.next) {
            process.stdout.write("->");
        }
    } while (curr !== tail.next);
}

// Driver Code
// Create the circular linked list: 2 -> 5 -> 7 -> 8 -> 10
let tail = new Node(10);
tail.next = new Node(2);
tail.next.next = new Node(5);
tail.next.next.next = new Node(7);
tail.next.next.next.next = new Node(8);
tail.next.next.next.next.next = tail;

const key = 1;

tail = insertAtEnd(tail, key);

printList(tail);

Output
2->5->7->8->10->1
Comment