Insertion in an empty List in the circular linked list Last Updated : 08 Aug, 2024 Comments Improve Suggest changes Like Article Like Report Try it on GfG Practice A circular linked list is a type of data structure where each node points to the next one, and the last node connects back to the first, forming a circle. This setup allows you to loop through the list without stopping. Knowing how to insert a node into an empty circular linked list is important in creating circular linked list.Insertion in an empty List in the circular linked listTo insert a node in empty circular linked list, creates a new node with the given data, sets its next pointer to point to itself, and updates the last pointer to reference this new node. Insertion in an empty ListStep-by-step approach:Check if last is not nullptr. If true, return last (the list is not empty).Otherwise, Create a new node with the provided data.Set the new node’s next pointer to point to itself (circular link).Update last to point to the new node and return it.Below is the implementation of the above approach: C++ #include <iostream> using namespace std; struct Node{ int data; Node *next; Node(int value){ data = value; next = nullptr; } }; // Function to insert a node into an empty circular singly linked list Node *insertInEmptyList(Node *last, int data){ if (last != nullptr) return last; // Create a new node Node *newNode = new Node(data); // Point newNode to itself newNode->next = newNode; // Update last to point to the new node last = newNode; return last; } void printList(Node* last){ if(last == NULL) return; // Start from the head node Node* head = last->next; while (true) { cout << head->data << " "; head = head->next; if (head == last->next) break; } cout << endl; } int main(){ Node *last = nullptr; // Insert a node into the empty list last = insertInEmptyList(last, 1); // Print the list cout << "List after insertion: "; printList(last); return 0; } C #include <stdio.h> #include <stdlib.h> // Define the Node structure struct Node { int data; struct Node* next; }; struct Node* createNode(int value); // Function to insert a node into an empty // circular singly linked list struct Node* insertInEmptyList(struct Node* last, int data) { if (last != NULL) return last; // Create a new node struct Node* newNode = createNode(data); // Update last to point to the new node last = newNode; return last; } void printList(struct Node* last) { if (last == NULL) return; // Start from the head node struct Node* head = last->next; while (1) { printf("%d ", head->data); head = head->next; if (head == last->next) break; } printf("\n"); } struct Node* createNode(int value) { struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); newNode->data = value; newNode->next = newNode; return newNode; } int main() { struct Node* last = NULL; // Insert a node into the empty list last = insertInEmptyList(last, 1); // Print the list printf("List after insertion: "); printList(last); return 0; } Java class Node { int data; Node next; Node(int value) { data = value; next = null; } } public class Main { // Function to insert a node into an empty // circular singly linked list static Node insertInEmptyList(Node last, int data) { if (last != null) return last; // Create a new node Node newNode = new Node(data); // Point newNode to itself newNode.next = newNode; // Update last to point to the new node last = newNode; return last; } // Function to print the list static void printList(Node last) { if (last == null) return; // Start from the head node Node head = last.next; while (true) { System.out.print(head.data + " "); head = head.next; if (head == last.next) break; } System.out.println(); } public static void main(String[] args) { Node last = null; // Insert a node into the empty list last = insertInEmptyList(last, 1); // Print the list System.out.print("List after insertion: "); printList(last); } } Python class Node: def __init__(self, value): self.data = value self.next = self # Point to itself def insertInEmptyList(last, data): if last is not None: return last # Create a new node new_node = Node(data) # Update last to point to the new node last = new_node return last def printList(last): if last is None: return # Start from the head node head = last.next while True: print(head.data, end=" ") head = head.next if head == last.next: break print() if __name__ == "__main__": last = None # Insert a node into the empty list last = insertInEmptyList(last, 1) # Print the list print("List after insertion: ", end="") printList(last) JavaScript class Node { constructor(value) { this.data = value; this.next = null; } } function insertInEmptyList(last, data) { if (last !== null) return last; // Create a new node let newNode = new Node(data); // Point newNode to itself newNode.next = newNode; // Update last to point to the new node last = newNode; return last; } function printList(last) { if (last === null) return; // Start from the head node let head = last.next; while (true) { console.log(head.data); head = head.next; if (head === last.next) break; } } // Main function let last = null; // Insert a node into the empty list last = insertInEmptyList(last, 1); // Print the list console.log("List after insertion:"); printList(last); OutputList after insertion: 1 Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Insertion in an empty List in the circular linked list H harendrakumar123 Follow Improve Article Tags : Linked List Data Structures DSA circular linked list Practice Tags : circular linked listData StructuresLinked List Similar Reads Insertion at the end in circular linked list A circular linked list is a data structure where each node points to the next, and the last node connects back to the first, creating a loop. Insertion at the end in circular linked list is an important operation. Understanding how to perform this insertion is essential for effectively manage and us 7 min read Insertion at the beginning in circular linked list A circular linked list is a special type of data structure where each node points to the next, and the last node connects back to the first, forming a loop. This design allows for continuous traversal without stopping. Inserting a node at the beginning of a circular linked list is an important opera 7 min read Insertion in Circular Singly Linked List In this article, we will learn how to insert a node into a circular linked list. Insertion is a fundamental operation in linked lists that involves adding a new node to the list. In a circular linked list, the last node connects back to the first node, creating a loop.There are four main ways to add 12 min read Insertion at specific position in circular linked list Inserting an element at a specific position in a circular linked list is a common operation that involves adjusting pointers in a circular structure. Unlike a regular linked list, where the last node points to NULL, a circular linked listâs last node points back to the head, forming a loop. This pro 10 min read Insertion in Doubly Circular Linked List Circular Doubly Linked List has properties of both doubly linked list and circular linked list in which two consecutive elements are linked or connected by the previous and next pointer and the last node points to the first node by the next pointer and also the first node points to the last node by 15+ min read Like