Given a linked list sorted in ascending order and an integer called key, insert data in the linked list such that the list remains sorted.
Input: Linked List: 25->36->47->58->69->80, key: 19
Output: 19->25->36->47->58->69->80
Explanation: After inserting 19 the sorted linked list will look like the one in the output.Input: Linked List: 50->100, key: 75
Output: 50->75->100
Explanation: After inserting 75 the sorted linked list will look like the one in the output.
Try It Yourself
[Expected Approach] Traverse and find the correct position for insertion - O(n) Time and O(1) Space
- If Linked list is empty then make the node as head and return it.
- If key < head->data, then insert the node at the start, make it head and return.
- 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 the head, keep moving until you reach a node who's next is greater than the key.
- Create a new node and make it next of the node found in the previous step.
#include <iostream>
using namespace std;
// Definition of a Linked List Node
class Node
{
public:
int data;
Node *next;
Node(int val)
{
data = val;
next = NULL;
}
};
// Function to insert a node in a sorted linked list
void sortedInsert(Node *&head, int key)
{
Node *newNode = new Node(key);
// Case 1: List is empty OR new value is smallest
if (head == NULL || key <= head->data)
{
newNode->next = head;
head = newNode;
return;
}
// Case 2: Find correct position
Node *current = head;
while (current->next != NULL && current->next->data < key)
{
current = current->next;
}
// Insert the node
newNode->next = current->next;
current->next = newNode;
}
// Function to print the linked list
void printList(Node *head)
{
Node *temp = head;
while (temp != NULL)
{
cout << temp->data << " ";
temp = temp->next;
}
}
// Driver code
int main()
{
Node *head = new Node(25);
head->next = new Node(36);
head->next->next = new Node(47);
head->next->next->next = new Node(58);
head->next->next->next->next = new Node(69);
head->next->next->next->next->next = new Node(80);
sortedInsert(head, 19);
printList(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
// Definition of a Linked List Node
typedef struct Node
{
int data;
struct Node *next;
} Node;
Node* createNode(int val){
Node* node = (Node*)malloc(sizeof(Node));
node->data = val;
node->next = NULL;
return node;
}
// Function to insert a node in a sorted linked list
void sortedInsert(Node **head, int key)
{
Node *newNode = createNode(key);
// Case 1: List is empty OR new value is smallest
if (*head == NULL || key <= (*head)->data)
{
newNode->next = *head;
*head = newNode;
return;
}
// Case 2: Find correct position
Node *current = *head;
while (current->next!= NULL && current->next->data < key)
{
current = current->next;
}
// Insert the node
newNode->next = current->next;
current->next = newNode;
}
// Function to print the linked list
void printList(Node *head)
{
Node *temp = head;
while (temp!= NULL)
{
printf("%d ", temp->data);
temp = temp->next;
}
}
// Driver code
int main()
{
Node *head = createNode(25);
head->next = createNode(36);
head->next->next = createNode(47);
head->next->next->next = createNode(58);
head->next->next->next->next = createNode(69);
head->next->next->next->next->next = createNode(80);
sortedInsert(&head, 19);
printList(head);
return 0;
}
import java.util.*;
// Definition of a Linked List Node
class Node {
int data;
Node next;
Node(int val)
{
data = val;
next = null;
}
}
public class GFG {
// Function to insert a node in a sorted linked list
static Node sortedInsert(Node head, int key)
{
Node newNode = new Node(key);
// Case 1: List is empty OR new value is smallest
if (head == null || key <= head.data) {
newNode.next = head;
head = newNode;
return head;
}
// Case 2: Find correct position
Node current = head;
while (current.next != null
&& current.next.data < key) {
current = current.next;
}
// Insert the node
newNode.next = current.next;
current.next = newNode;
return head;
}
// Function to print the linked list
static void printList(Node head)
{
Node temp = head;
while (temp != null) {
System.out.print(temp.data + " ");
temp = temp.next;
}
}
// Driver code
public static void main(String[] args)
{
Node head = new Node(25);
head.next = new Node(36);
head.next.next = new Node(47);
head.next.next.next = new Node(58);
head.next.next.next.next = new Node(69);
head.next.next.next.next.next = new Node(80);
head = sortedInsert(head, 19);
printList(head);
}
}
# Definition of a Linked List Node
class Node:
def __init__(self, val):
self.data = val
self.next = None
# Function to insert a node in a sorted linked list
def sortedInsert(head, key):
newNode = Node(key)
# Case 1: List is empty OR new value is smallest
if head is None or key <= head.data:
newNode.next = head
head = newNode
return head
# Case 2: Find correct position
current = head
while current.next is not None and current.next.data < key:
current = current.next
# Insert the node
newNode.next = current.next
current.next = newNode
return head
# Function to print the linked list
def printList(head):
temp = head
while temp is not None:
print(temp.data, end=" ")
temp = temp.next
# Driver code
if __name__ == "__main__":
head = Node(25)
head.next = Node(36)
head.next.next = Node(47)
head.next.next.next = Node(58)
head.next.next.next.next = Node(69)
head.next.next.next.next.next = Node(80)
head = sortedInsert(head, 19)
printList(head)
using System;
// Definition of a Linked List Node
class Node {
public int data;
public Node next;
public Node(int val)
{
data = val;
next = null;
}
}
class GFG {
// Function to insert a node in a sorted linked list
static void sortedInsert(ref Node head, int key)
{
Node newNode = new Node(key);
// Case 1: List is empty OR new value is smallest
if (head == null || key <= head.data) {
newNode.next = head;
head = newNode;
return;
}
// Case 2: Find correct position
Node current = head;
while (current.next != null
&& current.next.data < key) {
current = current.next;
}
// Insert the node
newNode.next = current.next;
current.next = newNode;
}
// Function to print the linked list
static void printList(Node head)
{
Node temp = head;
while (temp != null) {
Console.Write(temp.data + " ");
temp = temp.next;
}
}
// Driver code
static void Main(string[] args)
{
Node head = new Node(25);
head.next = new Node(36);
head.next.next = new Node(47);
head.next.next.next = new Node(58);
head.next.next.next.next = new Node(69);
head.next.next.next.next.next = new Node(80);
sortedInsert(ref head, 19);
printList(head);
}
}
// Definition of a Linked List Node
class Node {
constructor(val)
{
this.data = val;
this.next = null;
}
}
// Function to insert a node in a sorted linked list
function sortedInsert(head, key)
{
let newNode = new Node(key);
// Case 1: List is empty OR new value is smallest
if (head === null || key <= head.data) {
newNode.next = head;
head = newNode;
return head;
}
// Case 2: Find correct position
let current = head;
while (current.next !== null
&& current.next.data < key) {
current = current.next;
}
// Insert the node
newNode.next = current.next;
current.next = newNode;
return head;
}
// Function to print the linked list
function printList(head)
{
let temp = head;
let result = "";
while (temp !== null) {
result += temp.data + " ";
temp = temp.next;
}
console.log(result.trim());
}
// Driver code
let head = new Node(25);
head.next = new Node(36);
head.next.next = new Node(47);
head.next.next.next = new Node(58);
head.next.next.next.next = new Node(69);
head.next.next.next.next.next = new Node(80);
head = sortedInsert(head, 19);
printList(head);
Output
19 25 36 47 58 69 80

