Merge Sorted Linked Lists

#include
using namespace std;
class Node{
public:
int data;
Nodenext;
Node(int d)
{
data=d;
next=NULL;
}
};
void insert(Node
&head,int data)
{
if(head==NULL)
{
head=new Node(data);
}
Node*temp=head;
while(temp->next!=NULL)
{
temp=temp->next;

}
temp->next=new Node(data);
return;

}
void print(Nodehead)
{
while(head!=NULL)
{
cout<data<<" ";
head=head->next;
}
}
void linkedlist(Node
&head,int b)
{

int data;
cin>>data;

while(b–)
{

   insert(head,data);
   cin>>data;
   }	
   return;

}
Nodemerge(Nodehead1,Nodehead2)
{
if(head1==NULL)
{
return head2;
}
else if(head2==NULL)
{
return head1;
}
Node
c;
if(head1->datadata)
{
c=head1;
c->next=merge(head1->next,head2);
}
else{
c=head2;
c->next=merge(head1,head2->next);
}
return c;
}
int main() {
int n,t,k;
Nodehead=NULL;
Node
head1=NULL,*head2=NULL;
cin>>n;
while(n–){
cin>>t;
linkedlist(head1,t);

cin>>k;
linkedlist(head2,k);

Node*d=merge(head1,head2);
print(d);
}
return 0;

}
output=1 1 3 4 4 5 6 7
why 1 and 4 printing twice?

Hello @17manishms,

Always share your code using online coding blocks IDE.
The way you have shared it has introduced many syntax errors to it.
STEPS:

  1. Paste your code on https://ide.codingblocks.com
  2. Save it there.
  3. Share the URL generated.

Now, coming back to your code:
You have done two logical mistakes in linked list creation.
Suggestion:
Print both the linked lists to understand it better.

I have corrected your code:

Hope, this would help.
Give a like if you are satisfied.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.