question–>
https://practice.geeksforgeeks.org/problems/sort-a-linked-list/1
code–>
Node* mid1(Node* head){
Node* f=head;
Node* s=head;
while(f!=NULL and f->next!=NULL){
f=f->next->next;
s=s->next;
}
return s;
}
Node* Mergell(Node* head1,Node* head2){
if(head1==NULL){
return head2;
}
if(head2==NULL){
return head1;
}
Node* c=NULL;
if(head1->data<head2->data){
c=head1;
c->next=Mergell(head1->next,head2);
}
else{
c=head2;
c->next=Mergell(head1,head2->next);
}
return c;
}
Node* mergeSort(Node* head) {
// your code here
if(head==NULL || head->next==NULL){
return head;
}
Node* mid=mid1(head);
Node* a=head;
Node* b=mid->next;
mid->next=NULL;
a=mergeSort(a);
b=mergeSort(b);
Node* newnode=Mergell(a,b);
return newnode;
}
it is showing segmentation fault!!