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&lt;class t&gt;
Linkedlist&lt;t&gt; merge(Linkedlist&lt;t&gt; firstList, Linkedlist&lt;t&gt; secondList) {

/*4*/   Node&lt;int&gt; * current = ( firstList.head-&gt;data &lt; secondList.head-&gt;data ) ? firstList.head : secondList.head;
/*5*/   Node&lt;int&gt; * other = ( firstList.head-&gt;data &gt; secondList.head-&gt;data ) ? firstList.head : secondList.head;

    Node&lt;int&gt; * oldOther = nullptr;
    Node&lt;int&gt; * oldNext = nullptr;

    while ( current &amp;&amp; other ) {
        if ( current-&gt;next == nullptr || current-&gt;next-&gt;data &gt; other-&gt;data ) {
            oldOther = other;
            oldNext = current-&gt;next;
            current-&gt;next = other;
            other = oldNext;
            current = oldOther;
        } else {
            current = current-&gt;next;
        }
    }

    return ( firstList.head-&gt;data &lt; secondList.head-&gt;data ) ? firstList : secondList;
}
Your code has three issues I can see.....


the stated goal is to return a new list, but your code returns one of the input lists that have been modified to have the merged results. This is buggy.
If the first two elements in each list are equals, your initialization process will fail, one of the comparisons, either the first, or the second has to have an = component, either &lt;= or &gt;=. As it stands, both the current and the other will point to the secondList if the first values are equals.
What's Node&lt;int&gt;? Why is it not Node&lt;t&gt;? Why use templates at all if you have only one template type that works?