Java Program to Delete a Node From the Ending of the Circular Linked List Last Updated : 04 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 and then proceed according to the below approaches:Case 1: List is empty. If the list is empty we will simply return.Case 2: List is not empty delete():Return from the function if no node is presentSets both head and tail to null if only one node is thereif it has more than one node then it removes the previous head node, the head will point to the next node in the list and tail will point to the new head.printNode() will print all the nodes present in the list as:Node current is defined which will point to the headPrint current.val till it starts pointing to the head againIn each iteration, it will point to the next nodeCode: Java // Java Program to Delete a Node From the Ending of the // Circular Linked List public class Main { // Represents the node of list. public class Node { int val; Node next; public Node(int val) { this.val = val; } } // Initialising head and tail pointers public Node head = null; public Node tail = null; // add new node to the end public void addNode(int val) { // Creating new node Node node = new Node(val); // head and tail will point to new node // if list is empty if (head == null) { head = node; tail = node; node.next = head; } // otherwise tail point to new node and else { tail.next = node; tail = node; tail.next = head; } } // Deletes node from the end of the list public void delete() { // returns if list is empty if (head == null) { return; } // otherwise head will point to next element in the // list and tail will point to new head else { // if list contains more than one element // then loop will iterate // till second last element // is pointing to tail if (head != tail) { Node current = head; while (current.next != tail) { current = current.next; } // Second last element is the new tail tail = current; tail.next = head; } // if the list contains only one element // then both head and tail will point to null else { head = tail = null; } } } // displaying the nodes public void printNode() { Node current = head; if (head == null) { System.out.println("List is empty"); } else { do { System.out.print(" " + current.val); current = current.next; } while (current != head); System.out.println(); } } public static void main(String[] args) { Main list = new Main(); // Adds data to the list list.addNode(5); list.addNode(3); list.addNode(4); // Printing original list System.out.println("Original List: "); list.printNode(); // deleting node from ending // till the list is empty and // displaying the Updated list while (list.head != null) { list.delete(); // Printing updated list System.out.println("Updated List: "); list.printNode(); } } } OutputOriginal List: 5 3 4 Updated List: 5 3 Updated List: 5 Updated List: List is emptyTime complexity: O(n) where n is no of nodes of circular linked listAuxiliary Space: O(1) Comment More infoAdvertise with us Next Article Java Program to Insert a New Node at the Beginning of the Circular Linked List R rbbansal Follow Improve Article Tags : Java Java Programs Linked Lists Practice Tags : Java Similar Reads 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 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 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 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 Insert a New Node at the Beginning of the Circular Linked List 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 th 4 min read Java Program For Deleting A Node In A Linked List We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in the linked list. Iterative Method:To delete a node from 3 min read Like