> For the complete documentation index, see [llms.txt](https://gl01.gitbook.io/gfg-editorials/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gl01.gitbook.io/gfg-editorials/2024/02-2024-feb-3/05-sorted-insert-for-circular-linked-list.md).

# 05. Sorted insert for circular linked list

The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/sorted-insert-for-circular-linked-list/1)

## My Approach

* Create a new node with the given data.
* Check if the linked list is empty. If it is, make the new node the only node in the list.
* If the data in the new node is less than or equal to the data in the head node, insert the new node at the beginning.
* Otherwise, traverse the linked list to find the correct position for the new node.
* Insert the new node in the appropriate position.

## Time and Auxiliary Space Complexity

* **Time Complexity**: O(n) - where n is the number of nodes in the circular linked list.
* **Auxiliary Space Complexity**: O(1) - constant space is used.

## Code (C++)

```cpp
class Solution {
public:
    Node* sortedInsert(Node* head, int data) {
        Node* newNode = new Node(data);

        if (!head) {
            newNode->next = newNode;
            return newNode;
        }

        Node* curr = head;
        Node* next = curr->next;

        if (data <= curr->data) {
            newNode->next = head;
            while (curr->next != head)
                curr = curr->next;

            curr->next = newNode;
            return newNode;
        }

        while (next != head && data > next->data) {
            curr = next;
            next = next->next;
        }

        curr->next = newNode;
        newNode->next = next;

        return head;
    }
};
```

## Contribution and Support

For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment.

If you find this solution helpful, consider supporting us by giving a `⭐ star` to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://gl01.gitbook.io/gfg-editorials/2024/02-2024-feb-3/05-sorted-insert-for-circular-linked-list.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
