# Sorted Insert for Circular Linked List

## JAVA Program to Sort a Circular Linked List while Inserting Nodes in it

As we’ve learned in the earlier pages that Insertion can be done at various Positions in a Circular Linked List. In this page we’ll learn how we can use those techniques to create program using which we can insert nodes in a Circular Linked List and Sort the list Simultaneously. Below is a Solution in the form of a JAVA Program for Sorted Insert for Circular Linked List along with a detailed analysis of example.

## Algorithm that will be used in the Program below

• sortedInsert(Node nextNode)
• IF thisNode == null
• nextNode->next = nextNode
• ELSE IF (thisNode->element >= nextNode->element)
• FOR i=0 until thisNode->next != head
• thisNode = thisNode->next
• thisNode->next = nextNode
• ELSE
• WHILE thisNode->next != head && thisNode->next->element < nextNode-> element
• thisNode = thisNode->next
• nextNode->next = thisNode->next
• thisNode->next = nextNode
• EXIT
```void sortedInsert(int n)
{
Node nextNode=new Node(n);
if (thisNode == null)
{
nextNode.next = nextNode;
}
else if (thisNode.element >= nextNode.element)
{
for (int i=0; thisNode.next != head; i++)
thisNode = thisNode.next;
thisNode.next = nextNode;
}
else
{
while (thisNode.next != head && thisNode.next.element < nextNode.element)
thisNode = thisNode.next;
nextNode.next = thisNode.next;
thisNode.next = nextNode;
}
}
```

## Code for Sorted Insert In Circular Linked List in JAVA

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

## Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

## Checkout list of all the video courses in PrepInsta Prime Subscription

• Introduction to Circular Linked List
• Circular Linked List in –
C | C++ | Java
• Insertion in Circular Linked List –
C | C++ | Java
• Insertion at the beginning–
C | C++ | Java
• Insertion at the end –
C | C++ | Java
• Insertion at nth position –
C | C++ | Java
• Deletion in Circular Linked List –
C | C++ | Java
• Deletion from beginning in Circular Linked List –
C | C++ | Java
• Deletion from nth position in Circular Linked List –
• Deletion from end in Circular Linked List –
C | C++ | Java
• Insertion and Deletion in Circular Linked List – C | C++ | Java
• Split a Circular Linked List in two halves –
C | C++ | Java
• Count nodes in Circular Linked List –
C | C++ | Java
• Sorted Insert In Circular Linked List –
C | C++ | Java
• Insertion in the middle in Circular Linked List –
C | C++ | Java