Java Program to Sort the Elements of the Circular Linked List Last Updated : 20 Aug, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 list after sorting: SORTED CIRCULAR LINKED LIST Approach: Take two pointers: Current pointing to head of the node and Temp pointing to next node of Current.Now for each iteration compare value of Current pointer to the value of Temp pointer.Here two cases ariseCase 1: If the value of a current pointer is greater than that of Temp pointer Swap values of a current pointer and temp pointer. Move the temp pointer to next nodeCase 2: If the value of a current pointer is less than or equal to that of Temp pointer Move the temp pointer to next node Now keep doing this until temp.next !=head of the list.After completing step 3 move the Current to next node and repeat the steps 1,2,3 .Each iteration results in fixing of the shortest element of the list to it's correct position.Repeat the above steps until Current. Next != head of list . Let's see how this work for the first node of the given circular linked list Below is the implementation of the above approach: Java // Java Program to Sort the Elements // of the Circular Linked List import java.io.*; public class GFG { // Stores Information about Node of List public class Node { int data; Node next; public Node(int data) { this.data = data; } } // Declaring Head of the Node public Node head_of_node = null; // A last pointer to help append values to our list public Node last = null; // Add method adds values to the end of the list public void add(int data) { Node newNode = new Node(data); if (head_of_node == null) { head_of_node = newNode; last = newNode; newNode.next = head_of_node; } else { last.next = newNode; last = newNode; last.next = head_of_node; } } // Sort_List method sorts the circular // linked list Using the algorithm public void Sort_List() { // current pointer pointing to the head of the list Node current = head_of_node; // a temp pointer Node temp = null; // variable value helps in swap of the values int value; // this is the Algorithm discussed above if (head_of_node == null) { System.out.println("Your list is empty"); } else { while (current.next != head_of_node) { temp = current.next; while (temp != head_of_node) { if (current.data > temp.data) { value = current.data; current.data = temp.data; temp.data = value; } temp = temp.next; } current = current.next; } } } // Print_list method iterates through the list and // prints the values stored in the list public void Print_List() { Node current = head_of_node; if (head_of_node == null) { System.out.println("Your list is empty"); } else { do { System.out.print(" " + current.data); current = current.next; } while (current != head_of_node); System.out.println(); } } // Driver code public static void main(String[] args) { GFG circular_list = new GFG(); circular_list.add(10); circular_list.add(6); circular_list.add(3); circular_list.add(8); circular_list.add(4); System.out.print("Original List --> "); circular_list.Print_List(); circular_list.Sort_List(); System.out.print("List after Sorting--> "); circular_list.Print_List(); } } OutputOriginal List --> 10 6 3 8 4 List after Sorting--> 3 4 6 8 10 Time Complexity: O(N2) Auxiliary Space: O(1) as it is using constant space Comment More infoAdvertise with us Next Article Java Program to Sort the Elements of the Circular Linked List U uchiha1101 Follow Improve Article Tags : Java Practice Tags : Java Similar Reads C Program to Implement Circular Linked List A linked list is a dynamic data structure where elements are not stored in contiguous memory locations but linked using pointers. In a circular linked list, the last node points back to the first node instead of pointing to NULL, forming a circle.In this article, we will learn how to implement the c 8 min read Java Program for Merge Sort for Linked Lists Merge sort is often preferred for sorting a linked list. The slow random-access performance of a linked list makes some other algorithms (such as quicksort) perform poorly, and others (such as heapsort) completely impossible. Let head be the first node of the linked list to be sorted and headRef be 4 min read Circular Linked List Implementation in Java A Circular Linked List is a variation of the traditional linked list data structure. In the traditional linked list, the last node points to the null and it can indicating the end of the list. However, in the circular linked list, the last node points back to the first node and forms the circle or l 7 min read Why do we need circular linked lists? Circular linked lists are particularly useful in applications where we need to cycle through data repeatedly. They are efficient for certain types of problems and can be used in various applications such as implementation of a queue, music or media player, hash table implementation, memory allocatio 3 min read LinkedList descendingIterator() Method in Java In Java, the descendingIterator() method of LinkedList is used to return an iterator that allows to traverse the list in reverse order.Example 1: This example demonstrates how to use descendingIterator() method with Strings in a LinkedList.Java// Java program to use // descendingIterator() import ja 3 min read Like