Skip to main content
Rollback to Revision 1
Source Link
rolfl
  • 98.2k
  • 17
  • 220
  • 419

Routine to merge two linked lists together and return a new merged list. One optimization I can think of is passing by reference instead of value. Any other suggestions ? Is there a shorthand to make lines 4 and 5 single/shorter?

template<class t>
Linkedlist<t> merge(Linkedlist<t> firstList, Linkedlist<t> secondList) {
    
/*4*/   Node<t>Node<int> * current = ( firstList.head->data < secondList.head->data ) ? firstList.head : secondList.head;
/*5*/   Node<t>Node<int> * other = ( firstList.head->data > secondList.head->data ) ? firstList.head : secondList.head;
    
    Node<t>Node<int> * oldOther = nullptr;
    Node<t>Node<int> * oldNext = nullptr;
    
    while ( current && other ) {
        if ( current->next == nullptr || current->next->data > other->data ) {
            oldOther = other;
            oldNext = current->next;
            current->next = other;
            other = oldNext;
            current = oldOther;
        } else {
            current = current->next;
        }
    }
    
    return ( firstList.head->data < secondList.head->data ) ? firstList : secondList;
}

Routine to merge two linked lists together and return a new merged list. One optimization I can think of is passing by reference instead of value. Any other suggestions ? Is there a shorthand to make lines 4 and 5 single/shorter?

template<class t>
Linkedlist<t> merge(Linkedlist<t> firstList, Linkedlist<t> secondList) {
    
/*4*/   Node<t> * current = ( firstList.head->data < secondList.head->data ) ? firstList.head : secondList.head;
/*5*/   Node<t> * other = ( firstList.head->data > secondList.head->data ) ? firstList.head : secondList.head;
    
    Node<t> * oldOther = nullptr;
    Node<t> * oldNext = nullptr;
    
    while ( current && other ) {
        if ( current->next == nullptr || current->next->data > other->data ) {
            oldOther = other;
            oldNext = current->next;
            current->next = other;
            other = oldNext;
            current = oldOther;
        } else {
            current = current->next;
        }
    }
    
    return ( firstList.head->data < secondList.head->data ) ? firstList : secondList;
}

Routine to merge two linked lists together and return a new merged list. One optimization I can think of is passing by reference instead of value. Any other suggestions ? Is there a shorthand to make lines 4 and 5 single/shorter?

template<class t>
Linkedlist<t> merge(Linkedlist<t> firstList, Linkedlist<t> secondList) {
    
/*4*/   Node<int> * current = ( firstList.head->data < secondList.head->data ) ? firstList.head : secondList.head;
/*5*/   Node<int> * other = ( firstList.head->data > secondList.head->data ) ? firstList.head : secondList.head;
    
    Node<int> * oldOther = nullptr;
    Node<int> * oldNext = nullptr;
    
    while ( current && other ) {
        if ( current->next == nullptr || current->next->data > other->data ) {
            oldOther = other;
            oldNext = current->next;
            current->next = other;
            other = oldNext;
            current = oldOther;
        } else {
            current = current->next;
        }
    }
    
    return ( firstList.head->data < secondList.head->data ) ? firstList : secondList;
}
Removed leftover debug code
Source Link
Nash Vail
  • 245
  • 2
  • 9

Routine to merge two linked lists together and return a new merged list. One optimization I can think of is passing by reference instead of value. Any other suggestions ? Is there a shorthand to make lines 4 and 5 single/shorter?

template<class t>
Linkedlist<t> merge(Linkedlist<t> firstList, Linkedlist<t> secondList) {
    
/*4*/   Node<int>Node<t> * current = ( firstList.head->data < secondList.head->data ) ? firstList.head : secondList.head;
/*5*/   Node<int>Node<t> * other = ( firstList.head->data > secondList.head->data ) ? firstList.head : secondList.head;
    
    Node<int>Node<t> * oldOther = nullptr;
    Node<int>Node<t> * oldNext = nullptr;
    
    while ( current && other ) {
        if ( current->next == nullptr || current->next->data > other->data ) {
            oldOther = other;
            oldNext = current->next;
            current->next = other;
            other = oldNext;
            current = oldOther;
        } else {
            current = current->next;
        }
    }
    
    return ( firstList.head->data < secondList.head->data ) ? firstList : secondList;
}

Routine to merge two linked lists together and return a new merged list. One optimization I can think of is passing by reference instead of value. Any other suggestions ? Is there a shorthand to make lines 4 and 5 single/shorter?

template<class t>
Linkedlist<t> merge(Linkedlist<t> firstList, Linkedlist<t> secondList) {
    
/*4*/   Node<int> * current = ( firstList.head->data < secondList.head->data ) ? firstList.head : secondList.head;
/*5*/   Node<int> * other = ( firstList.head->data > secondList.head->data ) ? firstList.head : secondList.head;
    
    Node<int> * oldOther = nullptr;
    Node<int> * oldNext = nullptr;
    
    while ( current && other ) {
        if ( current->next == nullptr || current->next->data > other->data ) {
            oldOther = other;
            oldNext = current->next;
            current->next = other;
            other = oldNext;
            current = oldOther;
        } else {
            current = current->next;
        }
    }
    
    return ( firstList.head->data < secondList.head->data ) ? firstList : secondList;
}

Routine to merge two linked lists together and return a new merged list. One optimization I can think of is passing by reference instead of value. Any other suggestions ? Is there a shorthand to make lines 4 and 5 single/shorter?

template<class t>
Linkedlist<t> merge(Linkedlist<t> firstList, Linkedlist<t> secondList) {
    
/*4*/   Node<t> * current = ( firstList.head->data < secondList.head->data ) ? firstList.head : secondList.head;
/*5*/   Node<t> * other = ( firstList.head->data > secondList.head->data ) ? firstList.head : secondList.head;
    
    Node<t> * oldOther = nullptr;
    Node<t> * oldNext = nullptr;
    
    while ( current && other ) {
        if ( current->next == nullptr || current->next->data > other->data ) {
            oldOther = other;
            oldNext = current->next;
            current->next = other;
            other = oldNext;
            current = oldOther;
        } else {
            current = current->next;
        }
    }
    
    return ( firstList.head->data < secondList.head->data ) ? firstList : secondList;
}
Source Link
Nash Vail
  • 245
  • 2
  • 9

Merging two sorted linked lists C++

Routine to merge two linked lists together and return a new merged list. One optimization I can think of is passing by reference instead of value. Any other suggestions ? Is there a shorthand to make lines 4 and 5 single/shorter?

template<class t>
Linkedlist<t> merge(Linkedlist<t> firstList, Linkedlist<t> secondList) {
    
/*4*/   Node<int> * current = ( firstList.head->data < secondList.head->data ) ? firstList.head : secondList.head;
/*5*/   Node<int> * other = ( firstList.head->data > secondList.head->data ) ? firstList.head : secondList.head;
    
    Node<int> * oldOther = nullptr;
    Node<int> * oldNext = nullptr;
    
    while ( current && other ) {
        if ( current->next == nullptr || current->next->data > other->data ) {
            oldOther = other;
            oldNext = current->next;
            current->next = other;
            other = oldNext;
            current = oldOther;
        } else {
            current = current->next;
        }
    }
    
    return ( firstList.head->data < secondList.head->data ) ? firstList : secondList;
}