Google News
logo
Algorithm - Interview Questions
Write an algorithm to insert a node in a sorted linked list.
Algorithm to insert a node in a sorted linked list.
 
Case1 : Check if the linked list is empty then set the node as head and return it.
New_node-> Next= head;  
Head=New_node  
 
Case2 : Insert the new node in middle
While( P!= insert position)  
{  
P= p-> Next;  
}  
Store_next=p->Next;  
P->Next= New_node;  
New_Node->Next = Store_next;  

Case3 : Insert a node at the end
While (P->next!= null)  
{  
P= P->Next;  
}  
P->Next = New_Node;  
New_Node->Next = null;  
Advertisement