Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code of 2 sorted linked list merge #147

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 104 additions & 0 deletions Linked-List/sorted_merge_2LL.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include<stdio.h>
#include<stdlib.h>
struct node{
int data;
struct node *next;
};
struct node *create(struct node *head,int i){
struct node *ptr;
ptr=(struct node *) malloc(sizeof(struct node));
struct node *p;
printf("Enter value[%d] of list:",i);
scanf("%d",&ptr->data);
ptr->next=NULL;
if(head==NULL){
head=ptr;
p=head;
return(head);
}
else{
p->next=ptr;
p=ptr;
return(head);
}
}
struct node *merge(struct node * head1,struct node * head2){
struct node* p1=head1;
struct node *p2=head2;
struct node *dummy;
struct node *p3=dummy;
while(p1!=NULL&&p2!=NULL){
if(p1->data>p2->data){
p3->next=p2;
p2=p2->next;
}
else{
p3->next=p1;
p1=p1->next;
}
p3=p3->next;
}
while(p1!=NULL){
p3->next=p1;
p1=p1->next;
p3=p3->next;
}
while(p2!=NULL){
p3->next=p2;
p2=p2->next;
p3=p3->next;
}
return dummy->next;
}
struct node *merge_rec(struct node * head1,struct node *head2){
if(head1==NULL){
return head2;
}
if(head2==NULL){
return head1;
}
struct node *result;
if(head1->data<head2->data){
result=head1;
result->next=merge_rec(head1->next,head2);
}
else{
result=head2;
result->next=merge_rec(head1,head2->next);
}
return result;
}
void print(struct node *ptr){
int i=1;
while(ptr!=NULL){
printf("Value at Node[%d]:%d\n",i,ptr->data);
i++;
ptr=ptr->next;
}
}
int main(){
int i,n,data,pos;
struct node *head1=NULL;
struct node *head2=NULL;
printf("Enter size of linked list1:");
scanf("%d",&n);
for(i=0;i<n;i++){
head1=create(head1,i+1);
}
print(head1);

printf("Enter size of linked list2:");
scanf("%d",&n);
for(i=0;i<n;i++){
head2=create(head2,i+1);
}
print(head2);
printf("After merge two linked list:\n");
struct node *head=merge(head1,head2);
print(head);

/*printf("After merge two linked list by recursion:\n");
struct node *head_rec=merge_rec(head1,head2);
print(head_rec);*/
return 0;
}