Insert in a Sorted List | Practice | GeeksforGeeks

Given a linked list sorted in ascending order and an integer called data, insert data in the linked list such that the list remains sorted.
Example 1:
Input:
LinkedList: 25->36->47->58->69->80
data: 19
Output: 19 25 36 47 58 69 80

Example 2:
Input:
LinkedList: 50->100
data: 75
Output: 50 75 100
Your Task:
The task is to complete the function sortedInsert() which should insert the element in sorted Linked List and return the head of the linked list
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Constraints:
1 <= N <= 104
-99999 <= A[i] <= 99999, for each valid i
 
0
tthakare731 week ago
//java solution
class Solution {
    Node sortedInsert(Node head, int key) {
        // Add your code here.
        Node insert = new Node(key), dummy = head, pre = null;
        while(dummy != null && dummy.data <= key){
            pre = dummy;
            dummy = dummy.next;
        }
        
        if(pre != null)
            pre.next = insert;
        else 
            head = insert;
          
        insert.next = dummy;
        return head;
    }
}
0
swatisingh727773 weeks ago
 Node *sortedInsert(struct Node* head, int data) {
     
         Node* temp = new Node(data);
       if(head == NULL) {
           return temp;
       }
           if(head -> data > data) {
               temp -> next = head ;
             
               return temp;
           } 
   
       Node* curr = head;
       
       while(curr -> next != NULL && curr -> next -> data < data) {
           curr = curr -> next;
       }
       temp -> next = curr -> next;
       curr -> next = temp;
       return head;
       
   
    }
0
kuldeepy104594 weeks ago
Node *sortedInsert(struct Node* head, int data) {       // Code here       Node *newnode= new Node(data);       Node * curr= head;       Node *prev=NULL;       while(curr!=NULL){           if(curr->data<data){               prev=curr;               curr=curr->next;           }           else{               break;           }       }       if(prev){           prev->next=newnode;           newnode->next=curr;       }       else{          newnode->next=curr;           head = newnode;       }       return head;   }
0
nihalbaranwal
This comment was deleted.
0
sachinmishraravi1 month ago
JAVA
//please replace head1 to head
 class Solution {
    Node sortedInsert(Node head, int key) {
        // Add your code here.
        Node newNode= new Node(key);
        Node current=head,temp;
        if(current.data>newNode.data){
            newNode.next=current;
            return newNode;
        }
        while(current.next!=null){
        if(newNode.data>current.next.data){
            current=current.next;
        }
        else{
            // System.out.println(current.data);
         temp=current.next;
           current.next=newNode;
        newNode.next=temp;
        return head;
        }
        }
       current.next=newNode;
       return head; 
        
    }
}
0
prabhveer011 month ago
Node sortedInsert(Node head1, int key) {       // Add your code here.       Node current=head1;       Node temp=null;       Node newNode=new Node(key);       while(current!=null && current.data<key)       {        temp=current;        current=current.next;       }       try{       newNode.next=current;       temp.next=newNode;       }catch(Exception e)       {           newNode.next=head1;           head1=newNode;       }       return head1;                 }
0
as0042301 month ago
In C++;
 Node *sortedInsert(struct Node* head, int data) {       Node *ptr = new Node(data);       Node *cur = head;              if(head->data > data)       {         ptr->next = head;         head = ptr;         return head;       }              while(cur->next)       {           if(cur->next->data >= data){               ptr->next = cur->next;               cur->next = ptr;               return head;           }           cur = cur->next;       }              if(cur->data < data)       {           cur->next = ptr;           ptr->next = NULL;           return head;       }   }
0
mashhadihossain1 month ago
SIMPLE JAVA SOLUTION (3.5/7.6 SEC)
class Solution {   Node sortedInsert(Node head1, int key) {       Node temp=head1;       Node add=new Node(key);       if(key<temp.data)       {           add.next=temp;           return add;       }       else       {       while(temp.next!=null)       {           if(temp.next.data<key)           {               temp=temp.next;           }           else           {               Node curr=temp.next;               temp.next=add;               add.next=curr;               return head1;                          }       }       temp.next=add;       add.next=null;       return head1;       }   }}
+1
anuraggulati2411 month ago
C++ EASY SOLUTION 
 
class Solution{ public:   // Should return head of the modified linked list   Node *sortedInsert(struct Node* head, int data) {       // Code here       Node* temp = new Node(data);       if(head == NULL) {           return temp;       }           if(head -> data > data) {               temp -> next = head ;                            return temp;           }           Node* curr = head;              while(curr -> next != NULL && curr -> next -> data < data) {           curr = curr -> next;       }       temp -> next = curr -> next;       curr -> next = temp;       return head;          }};
0
roshantekam10202 months ago
Node sortedInsert(Node head1, int key) {       // Add your code here.       Node node=new Node(key);                     Node temp=head1;       Node t=temp;       if(key<temp.data)       {               node.next=head1;               return node;       }     else     {         boolean flag=true;       while(temp!=null)       {           if(key<temp.data)           {               flag=false;               t.next=node;               node.next=temp;               break;           }           t=temp;           temp=temp.next;       }       if(flag==true)       {           t.next=node;           node.next=null;       }       return head1;     }   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.