Sorted insert for circular linked list | Practice | GeeksforGeeks

Given a sorted circular linked list, the task is to insert a new node in this circular list so that it remains a sorted circular linked list.
Example 1:
Input:
LinkedList = 1->2->4
(the first and last node is connected,
i.e. 4 --> 1)
data = 2
Output: 1 2 2 4

Example 2:
Input:
LinkedList = 1->4->7->9
(the first and last node is connected,
i.e. 9 --> 1)
data = 5
Output: 1 4 5 7 9

Your Task:
The task is to complete the function sortedInsert() which should insert the new node into the given circular linked list and return the head of the linkedlist.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
0 <= N <= 105
0
shakshamkaushik12 weeks ago
 
public static Node sortedInsert(Node head,int data)
        {
            Node temp = head;
            Node p = null;
            while (temp.next.data<data){
                temp = temp.next;
                if(temp.next == head){
                    break;
                }
            }
            p = new Node(data);
            if(data>temp.data && data<=temp.next.data){
                p.next = temp.next;
                temp.next = p;
            }else{
                while(temp.next!=head)
                    temp = temp.next;
//= p.next;
                    p.next = temp.next;
                    temp.next = p;
                    head = p;
                
                
        }
        return head;
        }
0
alokkumar90194 weeks ago
Node *sortedInsert(Node* head, int data)
   {
      //Your code here
      Node *temp=new Node(data);
      
      if(head==NULL){
         temp->next=temp;
         return temp;
      }
      if(head->data>data){
          temp->next=head->next;
          head->next=temp;
          int t=head->data;
          head->data=temp->data;
          temp->data=t;
          return head;
      }
      Node *curr=head;
      while(curr->next!=head && curr->next->data<data){
          curr=curr->next;
      }
      temp->next=curr->next;
      curr->next=temp;
      return head;
   }
0
sparhibsp20181 month ago
// my code work for all cases but on submit it says segment fault , and i test this cases which say error in run time  , and it perfectly works on compile but it shows error in submit . 
//Your code here       Node* first=head;       Node* sec;       if(first->data<=data)       {           first=first->next;           while(first!=head)           {               if(first->data<=data)               {                   sec=first;                   first=first->next;               }               else               {                   break;               }           }                 Node *newnode= new Node(data);           newnode->next=first;           sec->next=newnode;           return head;       }       else{           Node *newnode=new Node(data);           while(first->next!=head){               first=first->next;           }           first->next=newnode;           newnode->next=head;           return newnode;       }
0
aryansinha18182 months ago
A solution with explained test cases
 
Node *sortedInsert(Node* head, int data)   {      //Your code here      Node* temp = new Node(data);            // Case 1. When there is no node and the inserted node is the new node      if(head == NULL){          head = temp;          temp -> next = head;          return head;      }      //Case 2. When insertion is done before first node in circular LL      Node*  curr = head;            if(data<curr -> data){          temp -> next = head;          while(curr->next != head){              curr = curr -> next;          }          curr -> next  = temp;          head = temp;          return head;      }      else{       //Case 3. When insertion is done anywhere between the circular LL       while(curr -> data < data && curr -> next -> data < data){           curr = curr->next;       }       Node* nxt = curr -> next;       curr -> next = temp;       temp -> next = nxt;       return head;      }   }
+1
anuraggulati2412 months ago
Simple C++ solution 
 
 
 public:
    Node *sortedInsert(Node* head, int data)
    {
       //Your code here
        //code here
    Node* temp = new Node(data);
    
    // edege case 
    // zero node
    if(head == NULL) {
        head = temp;
        temp -> next = temp;
        return head;
    }
    
    Node* curr = head;
    
    if(data < curr -> data ) {
        temp -> next = head;
        while(curr -> next != head) {
            curr = curr -> next;
        }
        curr -> next = temp;
        return temp;
    }
    // one node...
   else {
       while(curr -> data < data && curr -> next -> data < data) {
           curr = curr -> next;
       }
       Node* nxt = curr -> next;
       curr -> next = temp;
       temp -> next = nxt;
       return head;
   }
    }
0
rafi19982 months ago
class Solution
{
	public static Node sortedInsert(Node head,int data)
         {
            Node temp=head, t=null;
            
            while(temp.next.data<data){
                temp=temp.next;
                if(temp.next==head){
                    break;
                }
            }
            t=new Node(data);
            if(data>temp.data && data<=temp.next.data){
                t.next=temp.next;
                temp.next=t;
            }
            else{
                while(temp.next!=head)
                    temp=temp.next;
                t.next=temp.next;
                temp.next=t;
                head=t;
            }
        
            return head;
         }
}
+1
mashhadihossain2 months ago
SIMPLE JAVA SOLUTION (1.4/2.6 SEC)
class Solution{public static Node sortedInsert(Node head,int data)        {           Node temp=head;                      HashSet<Node> set=new HashSet<Node>();           boolean flag=false;           while(!set.contains(temp))           {               set.add(temp);               if(data>temp.data)               {                  flag=true;                }               else               {                   flag=false;               }               temp=temp.next;           }           if(!flag)           {               Node temp2=head;               HashSet<Node> set1=new HashSet<Node>();                              if(set.size()==1)               {                   if(data<temp2.data)                   {                       Node val=new Node(data);                       val.next=temp2;                       temp2.next=val;                       return val;                   }                   else                   {                      Node val=new Node(data);                      temp2.next=val;                      val.next=temp2;                      return temp2;                   }                                  }                              if(head.data>data && set.size()>1)               {                  int x=head.data;                                   temp.data=data;                                   data=x;                                  }                                                            while(!set1.contains(temp2))               {                   set1.add(temp2);                   if(temp2.next.data<data)                   {                       temp2=temp2.next;                   }                   else if(temp2.next.data>=data)                   {                       Node curr=temp2.next;                      Node extra=new Node(data);                      temp2.next=extra;                      extra.next=curr;                      return head;                   }               }           }           else           {               Node temp3=head;               HashSet<Node> set2=new HashSet<Node>();               while(!set2.contains(temp3.next))               {                   set2.add(temp3);                   temp3=temp3.next;               }               Node curr=temp3.next;               Node extra=new Node(data);               temp3.next=extra;               extra.next=curr;                          }           return head;        }}
+2
badgujarsachin832 months ago
 Node *sortedInsert(Node* head, int data)
    {
       //Your code here
       Node* newnode=new Node(data);
       Node* temp=head,*temp2=head;
       if(!head){
           newnode->next=newnode;
           return newnode;
       }else if(data<=head->data){
           newnode->next=head;
           while(temp->next!=head){
               temp=temp->next;
           }
           temp->next=newnode;
           return newnode;
       }else{
           while(temp->next!=head && data>temp->next->data){
               temp=temp->next;
           }
           newnode->next=temp->next;
           temp->next=newnode;
           return head;
       }
    }
-3
mayank20212 months ago
C++Node *sortedInsert(Node* head, int data)   {      Node *newnode=new Node(data);      Node *temp=head, *temp1=head;      if(!head)      {       newnode->next=newnode;       return newnode;      }      else if (data <= head->data)       {           newnode->next=head;           while(temp->next !=head)              temp=temp->next;              temp->next=newnode;              //head=newnode;            return newnode;       }       else       {           while(temp->next !=head && data > temp->next->data)              {              temp=temp->next;              }           newnode->next=temp->next;              temp->next=newnode;           return head;       }         }
-1
gdhikshith13 months ago
Node *sortedInsert(Node* head, int data)   {      struct Node* p=head;   if(head==NULL)   {       struct Node* p=new Node(data);       p->next=p;       return p;   }   p=head;   if(data<=head->data)   {       p=new Node(data);       p->next=head;       struct Node* q=head;       while(q->next!=head)       {           q=q->next;       }       q->next=p;       head=p;       return head;   }   struct Node* r=NULL;   p=head;   while(p->next!=head)   {       if(data<=p->data)       {           struct Node* temp=new Node(data);           r->next=temp;           temp->next=p;           return head;       }       else       {           r=p;       p=p->next;       }   }   struct Node* w=new Node(data);         p->next=w;         w->next=head;       return head;   }
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.