# Given a linked list which is sorted, how will you insert in sorted way

Given a sorted linked list and a value to insert, write a function to insert the value in sorted way.

Linked List after insertion of 9

## Recommended: Please solve it on “PRACTICE” first, before moving on to the solution.

Algorithm:
Let input linked list is sorted in increasing order.

```1) If Linked list is empty then make the node as
2) If value of the node to be inserted is smaller
than value of head node, then insert the node
at start and make it head.
3) In a loop, find the appropriate node after
which the input node (let 9) is to be inserted.
To find the appropriate node start from head,
keep moving until you reach a node GN (10 in
the below diagram) who's value is greater than
the input node. The node just before GN is the
appropriate  node (7).
4) Insert the node (9) after the appropriate node
(7) found in step 3.
```

Implementation:

## C/C++

```/* Program to insert in a sorted list */
#include<stdio.h>
#include<stdlib.h>

struct Node
{
int data;
struct Node* next;
};

/* function to insert a new_node in a list. Note that this
function expects a pointer to head_ref as this can modify the
void sortedInsert(struct Node** head_ref, struct Node* new_node)
{
struct Node* current;
/* Special case for the head end */
{
}
else
{
/* Locate the node before the point of insertion */
while (current->next!=NULL &&
current->next->data < new_node->data)
{
current = current->next;
}
new_node->next = current->next;
current->next = new_node;
}
}

/* BELOW FUNCTIONS ARE JUST UTILITY TO TEST sortedInsert */

/* A utility function to create a new node */
struct Node *newNode(int new_data)
{
/* allocate node */
struct Node* new_node =
(struct Node*) malloc(sizeof(struct Node));

/* put in the data  */
new_node->data  = new_data;
new_node->next =  NULL;

return new_node;
}

/* Function to print linked list */
{
while(temp != NULL)
{
printf("%d  ", temp->data);
temp = temp->next;
}
}

/* Drier program to test count function*/
int main()
{
struct Node *new_node = newNode(5);
new_node = newNode(10);
new_node = newNode(7);
new_node = newNode(3);
new_node = newNode(1);
new_node = newNode(9);

return 0;
}
```

## Java

```// Java Program to insert in a sorted list
{

class Node
{
int data;
Node next;
Node(int d) {data = d; next = null; }
}

/* function to insert a new_node in a list. */
void sortedInsert(Node new_node)
{
Node current;

/* Special case for head node */
{
}
else {

/* Locate the node before point of insertion. */

while (current.next != null &&
current.next.data < new_node.data)
current = current.next;

new_node.next = current.next;
current.next = new_node;
}
}

/*Utility functions*/

/* Function to create a node */
Node newNode(int data)
{
Node x = new Node(data);
return x;
}

/* Function to print linked list */
void printList()
{
while (temp != null)
{
System.out.print(temp.data+" ");
temp = temp.next;
}
}

/* Drier function to test above methods */
public static void main(String args[])
{
Node new_node;
new_node = llist.newNode(5);
llist.sortedInsert(new_node);
new_node = llist.newNode(10);
llist.sortedInsert(new_node);
new_node = llist.newNode(7);
llist.sortedInsert(new_node);
new_node = llist.newNode(3);
llist.sortedInsert(new_node);
new_node = llist.newNode(1);
llist.sortedInsert(new_node);
new_node = llist.newNode(9);
llist.sortedInsert(new_node);
llist.printList();
}
}
/* This code is contributed by Rajat Mishra */
```

## Python

```
# Python program to insert in sorted list

# Node class
class Node:

# Constructor to initialize the node object
def __init__(self, data):
self.data = data
self.next = None

def __init__(self):

def sortedInsert(self, new_node):

# Special case for the empty linked list

# Special case for head at end

else :

# Locate the node before the point of insertion
while(current.next is not None and
current.next.data < new_node.data):
current = current.next

new_node.next = current.next
current.next = new_node

# Function to insert a new node at the beginning
def push(self, new_data):
new_node = Node(new_data)

def printList(self):
while(temp):
print temp.data,
temp = temp.next

# Driver program
new_node = Node(5)
llist.sortedInsert(new_node)
new_node = Node(10)
llist.sortedInsert(new_node)
new_node = Node(7)
llist.sortedInsert(new_node)
new_node = Node(3)
llist.sortedInsert(new_node)
new_node = Node(1)
llist.sortedInsert(new_node)
new_node = Node(9)
llist.sortedInsert(new_node)
llist.printList()

# This code is contributed by Nikhil Kumar Singh(nickzuck_007)

```

Output:
```Created Linked List
1 3 5 7 9 10 ```

Shorter Implementation using double pointers
Thanks to Murat M Ozturk for providing this solution. Please see Murat M Ozturk’s comment below for complete function. The code uses double pointer to keep track of the next pointer of the previous node (after which new node is being inserted).

Note that below line in code changes current to have address of next pointer in a node.

```   current = &((*current)->next);
```

```    /* Copies the value-at-address current to
new_node's next pointer*/
new_node->next = *current;

/* Fix next pointer of the node (using it's address)
after which new_node is being inserted */
*current = new_node;
```

Time Complexity: O(n)

# GATE CS Corner    Company Wise Coding Practice

1.6 Average Difficulty : 1.6/5.0
Based on 176 vote(s)