# Sorted insert for circular linked list

• Difficulty Level : Easy
• Last Updated : 26 Jul, 2022

Difficulty Level: Rookie
Write a C function to insert a new value in a sorted Circular Linked List (CLL). For example, if the input CLL is following.

Algorithm:

Allocate memory for the newly inserted node and put data in the newly allocated node. Let the pointer to the new node be new_node. After memory allocation, following are the three cases that need to be handled.

a)  since new_node is the only node in CLL, make a self loop.
new_node->next = new_node;
b) change the head pointer to point to new node.
2) New node is to be inserted just before the head node:
(a) Find out the last node using a loop.
current = current->next;
(b) Change the next of last node.
current->next = new_node;
(c) Change next of new node to point to head.
(d) change the head pointer to point to new node.
3) New node is to be  inserted somewhere after the head:
(a) Locate the node after which new node is to be inserted.
current->next->data < new_node->data)
{   current = current->next;   }
(b) Make next of new_node as next of the located pointer
new_node->next = current->next;
(c) Change the next of the located pointer
current->next = new_node;

## Javascript



Output:

1 2 11 12 56 90

Time Complexity: O(n)

Here n is the number of nodes in the given linked list.

Auxiliary Space: O(1).

As constant extra space is used.

Case 2 of the above algorithm/code can be optimized. To implement the suggested change we need to modify case 2 to follow.

## C

 // Case 2 of the above algoelse if (current->data >= new_node->data){  // swap the data part of head node and new node  // assuming that we have a function swap(int *, int *)  swap(&(current->data), &(new_node->data));    new_node->next = (*head_ref)->next;  (*head_ref)->next = new_node;}

## C++

 // Case 2 of the above algo  else if (current->data >= new_node->data)  {    // swap the data part of head node and new node    // assuming that we have a function swap(int *, int *)    swap(&(current->data), &(new_node->data));       new_node->next = (*head_ref)->next;    (*head_ref)->next = new_node;  }// this code is contributed by devendra salunke

## Java

 // Case 2 of the above algoelse if (current.data >= new_node.data){  // swap the data part of head node and new node  // assuming that we have a function swap(int *, int *)  Node tmp = current.data;  current.data = new_node.data;  new_node.data = tmp;  new_node.next = (head_ref).next;  (head_ref).next = new_node;}  // This code is contributed by pratham76.

## Python3

 # Case 2 of the above algoelif (current.data >= new_node.data):      # swap the data part of head node and new node  # assuming that we have a function swap(int *, int *)  tmp = current.data;  current.data = new_node.data;  new_node.data = tmp;  new_node.next = (head_ref).next;  (head_ref).next = new_node;  # This code is contributed by _saurabh_jaiswal

## C#

 // Case 2 of the above algoelse if (current.data >= new_node.data){  // swap the data part of head node and new node  // assuming that we have a function swap(int *, int *)  Node tmp = current.data;  current.data = new_node.data;  new_node.data = tmp;  new_node.next = (head_ref).next;  (head_ref).next = new_node;}  // This code is contributed by rutvik_56

## Javascript



In the previous approach the time complexity of case 2 was O(n) which is reduced to O(1) using this approach.

The auxiliary space used here is O(1) which is same as previous approach.

Please write comments if you find the above code/algorithm incorrect, or find other ways to solve the same problem.

My Personal Notes arrow_drop_up