I am trying to merge two linked lists in c++ without in place. But everytime the new list created emits out the following numbers after it reaches the nullptr for one or the other list.
MyLinkedList mergeTwoLinkedList(MyLinkedList b) {
MyLinkedList c;
node *tempa = this->head;
node *tempb = b.head;
if (b.head == nullptr)
return *this;
if (this->head == nullptr)
return b;
if (tempa && tempb) {
do
{
if ((tempa->val) <= (tempb->val)) {
c.addAtTail(tempa->val);
tempa = tempa->next;
} else if ((tempa->val) >= (tempb->val)) {
c.addAtTail(tempb->val);
tempb = tempb->next;
}
}
while (tempa && tempb);
return c;
}
}