Skip to content

Latest commit

 

History

History
40 lines (31 loc) · 901 Bytes

File metadata and controls

40 lines (31 loc) · 901 Bytes
 Node *sortedInsert(struct Node* head, int data) {
        
        if(head==NULL || head->data>=data)
        {
            Node* node=new Node(data);
            node->next=head;
            head=node;
            
            return head;
        }
        
        int flag=0;
        
        Node* t=head;
        Node* node = new Node(data);
        while(t->next!=NULL)
        {
            if(t->data <= data && t->next->data>=data)
            {
                node->next=t->next;
                t->next=node;
                flag=1;
                break;
            }
            t=t->next;
        }
        
        if(flag==1)
            return head;
        
        if(t->data<data)
            t->next=node;
            
        return head;
    }