# Inserting a Node in Sorted List

### Procedure :

* Create a new node and assign the  data to it.
* Check if the head node is NULL. If it is, no nodes are present in the linked list  and the new node will be the head.
* Check if the head node's data is greater than the data to be inserted. If so, the new node will become the head node.
* If nodes are already present, reach the proper position by checking if the data of next node in each iteration is less than the data to be inserted.
* Once the position is reached, set the next of new node to be the next of current node.
* Finally, set next of current node to new node.

```c
void insert(struct node *ptr, int data) {

    // Creating a node
    struct node *newNode;
    newNode = (struct node *)malloc(sizeof(struct node));
    newNode -> data = data;

    // If there are no nodes or first node data is greater
    if (head == NULL || head -> data > data) {
        newNode -> next = head;
        head = newNode;        
    }
    
    // Inserting at correct position
    else {
        while (ptr -> next != NULL && ptr -> next -> data < data) {
           ptr = ptr -> next;
       }
       newNode -> next = ptr -> next;
       ptr -> next = newNode;
    }

}
```

Contributed by Nitin Ranganath


---

# Agent Instructions: 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://nitinranganath.gitbook.io/data-structures/linked-list/inserting-a-node-in-sorted-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.
