Java Program to Insert a New Node at the Beginning of the Circular Linked List Last Updated : 15 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Algorithm : 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).Create another class for creating the circular linked list and it has two nodes namely head and tail.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.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 // 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) { this.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. tail.next = head; } else { // point the newnode to head newNode.next = head; // point the rail to new node. tail.next = 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 { System.out.println("\n"); // 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(" " + presentNode.data); // incrementing the nodes using next // property. presentNode = presentNode.next; // if present node is head, stop printing // the nodes. } while (presentNode != head); } } public static void main(String[] args) { AddAtBeginning obj = new AddAtBeginning(); System.out.println( "Adding nodes at the beginning of the list: "); obj.addNode('s'); obj.printLinkedList(); // add k at the beginning obj.addNode('k'); obj.printLinkedList(); // add e at the beginning obj.addNode('e'); obj.printLinkedList(); // add e at the beginning obj.addNode('e'); obj.printLinkedList(); // add G at the beginning obj.addNode('G'); obj.printLinkedList(); } } OutputAdding nodes at the beginning of the list: s 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 Comment More infoAdvertise with us Next Article Java Program to Delete a Node From the Middle of the Circular Linked List P pulamolusaimohan Follow Improve Article Tags : Java Technical Scripter Java Programs Technical Scripter 2020 Practice Tags : Java Similar Reads Java Program to Insert a New Node at the Middle of the Circular Linked List Given a Circular Linked List, the task is to add a New Node at the Middle of the List. Let's consider the following Circular Linked List: List-Before-InsertionList-After-InsertionCreate a new node (New_node).Check for an empty list. If the list is empty then insert the node as head.For non-empty lis 3 min read Java Program to Delete a Node From the Beginning of the Circular Linked List In this article, we will learn about deleting a node from the beginning of a circular linked list. Consider the linked list as shown below. Example: Input : 5->3->4->(head node) Output: 3->4->(head node) Two cases arrive while solving the problem, Case 1: List is empty If the list is 3 min read Java Program to Delete a Node From the Middle of the Circular Linked List In this article, we are going to learn to delete the middle node from the circular Linked List in java. The approach we are going to follow for this program is, first we calculate the number of nodes in the list and then divide the number of nodes by 2 to get the middle node of the list. Algorithm C 4 min read Java Program to Delete a Node From the Ending of the Circular Linked List In this article, we will learn about deleting a node from the ending of a circular linked list. Consider the linked list as shown below:Â Example:Input : 5->3->4->(head node)Output: 5->3->(head node)We will first initialize the list and add some data into it with the addNode() method a 3 min read Java Program For Inserting Node In The Middle Of The Linked List Given a linked list containing n nodes. The problem is to insert a new node with data x at the middle of the list. If n is even, then insert the new node after the (n/2)th node, else insert the new node after the (n+1)/2th node. Examples: Input : list: 1->2->4->5 x = 3 Output : 1->2-> 5 min read Java Program to Sort the Elements of the Circular Linked List In a circular linked list, every node points to its next node in the sequence but the last node points to the first node in the list. Here, Create a circular linked list and sort the circular linked list in ascending order. Circular linked list before sorting: CIRCULAR LINKED LIST Circular linked li 3 min read Like