Whats wrong with this logic?

#include
using namespace std;

class node
{
public:
int data;
node *next;

//Constructor
node(int d)
{
    data = d;
    next = NULL;
}

};

void insertAtHead(node *&head, int data)
{
node *n = new node(data);
n->next = head;
head = n;
}

void insertAtTail(node &head, int data)
{
if(head == NULL){
node
n = new node(data);
head = n;
return;
}
node *temp = head;
while (temp->next != NULL)
{
temp = temp->next;
}
node *n = new node(data);
temp->next = n;
}

node mergeSortedLinkedList(nodehead, nodehead1){
node
newHead = NULL;
while(head->next != NULL && head1->next != NULL){
if((head->data > head1->data) || (head->next == NULL)){
insertAtTail(newHead, head1->data);
head1 = head1->next;

    }
    else{
        insertAtTail(newHead, head->data);
        head = head->next;
    }
}
return newHead;

}

void print(node *head)
{
node *temp = head;
while (temp != NULL)
{
cout << temp->data << ", ";
temp = temp->next;
}
cout << endl;
}

int main()
{
node *head = NULL;
insertAtHead(head, 10);
insertAtHead(head, 8);
insertAtHead(head, 6);
insertAtHead(head, 2);

node *head1 = NULL;
insertAtHead(head, 9);
insertAtHead(head, 7);
insertAtHead(head, 3);
insertAtHead(head, 1);

print(head);
print(head1);

node*mergeHead = mergeSortedLinkedList(head, head1);
print(mergeHead);



return 0;

}

Hello @simmy_gupta0404,

Always share your code using some Online IDE.
The way you share it has introduced many syntax errors to it.

STEPS:

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

Hello @simmy_gupta0404,

I have corrected your code:


Refer to comments for better understanding.

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.