Skip to main content
edited body
Source Link
Floella
  • 207
  • 2
  • 7

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){
                    Nodo*Node* aux = list1->next;
                    list1->next = list2;
                    list2 = aux;
                }
                list1 = list1->next;
            }
            if (list1->next == nullptr){
                list1->next = list2;
            }
            return newList;
        }
    }
}

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){
                    Nodo* aux = list1->next;
                    list1->next = list2;
                    list2 = aux;
                }
                list1 = list1->next;
            }
            if (list1->next == nullptr){
                list1->next = list2;
            }
            return newList;
        }
    }
}

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;
        }
    }
}
Source Link
Floella
  • 207
  • 2
  • 7

C++ merging sorted linked lists

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){
                    Nodo* aux = list1->next;
                    list1->next = list2;
                    list2 = aux;
                }
                list1 = list1->next;
            }
            if (list1->next == nullptr){
                list1->next = list2;
            }
            return newList;
        }
    }
}