I'd like to see if there's any code reuse I'm missing, or anything else that can be rewritten in a better way, but always following the same data structures (without adding more classes or using the C++ list implementation).
Node* merge(Node* list1, Node* list2){
if (list1 == nullptr){
return list2;
}
else{
if (list2 == nullptr){
return list1;
}
else{
Node* newList = nullptr;
if (list1->data <= list2->data){
newList = list1;
}
else{
newList = list2;
}
while (list1->next != nullptr) {
if (list1->next->data > list2->data){
Node* aux = list1->next;
list1->next = list2;
list2 = aux;
}
list1 = list1->next;
}
if (list1->next == nullptr){
list1->next = list2;
}
return newList;
}
}
}
Nodeso we don't have to infer it - even better would be a complete program with amain()to run a few tests, then we could verify our suggestions before posting. \$\endgroup\$