Insert in a Sorted List
PROBLEM :
Given a sorted singly linked list and a data, your task is to complete the function sortedInsert that inserts the data in the linked list in sorted way i.e. order of the list doesn't changes.
Input:
The function takes 2 arguments as input, reference pointer to the head of the sorted single linked list and an integer data value which is to be inserted in the list.
There are multiple test cases and for each test case the function will be called separately.
Output:
For each test output will be space separated integers denoting the values of the linked list.
Constraints:
1<=T<=100
0<=N<=100
-999<=A[]<=999
Example:
Input:
2
6
25 36 47 58 69 80
19
2
50 100
75
Output:
19 25 36 47 58 69 80
50 75 100
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
structure of the node of the list is as
struct node
{
int data;
struct node* next;
};
*/
void sortedInsert(struct node** head_ref, int data)
{
struct node *curr ;
curr=(struct node *)malloc(sizeof(struct node)) ;
curr->data=data ;
curr->next=NULL ;
if(curr->data<(*head_ref)->data){
curr->next=(*head_ref) ;
(*head_ref)=curr ;
return ;
}
struct node *temp ;
temp=(*head_ref) ;
while(temp->next&&temp->next->data<curr->data)
temp=temp->next ;
if(temp->next==NULL){
temp->next=curr ;
return ;
}
curr->next=temp->next ;
temp->next=curr ;
return ;
}
--------------------------------------------------------------------------------
Given a sorted singly linked list and a data, your task is to complete the function sortedInsert that inserts the data in the linked list in sorted way i.e. order of the list doesn't changes.
Input:
The function takes 2 arguments as input, reference pointer to the head of the sorted single linked list and an integer data value which is to be inserted in the list.
There are multiple test cases and for each test case the function will be called separately.
Output:
For each test output will be space separated integers denoting the values of the linked list.
Constraints:
1<=T<=100
0<=N<=100
-999<=A[]<=999
Example:
Input:
2
6
25 36 47 58 69 80
19
2
50 100
75
Output:
19 25 36 47 58 69 80
50 75 100
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
/*
structure of the node of the list is as
struct node
{
int data;
struct node* next;
};
*/
void sortedInsert(struct node** head_ref, int data)
{
struct node *curr ;
curr=(struct node *)malloc(sizeof(struct node)) ;
curr->data=data ;
curr->next=NULL ;
if(curr->data<(*head_ref)->data){
curr->next=(*head_ref) ;
(*head_ref)=curr ;
return ;
}
struct node *temp ;
temp=(*head_ref) ;
while(temp->next&&temp->next->data<curr->data)
temp=temp->next ;
if(temp->next==NULL){
temp->next=curr ;
return ;
}
curr->next=temp->next ;
temp->next=curr ;
return ;
}
--------------------------------------------------------------------------------
Comments
Post a Comment