title: Why do I keep getting this error when merging two sorted linked lists in C++?
body: <p>I am writing a function to merge two sorted linked lists and return the sorted list. I keep getting this error. SIGABRT - Abort (abnormal termination) signal</p>
  <pre><code>
  template &lt;typename T&gt;
  LinkedList&lt;T&gt; LinkedList&lt;T&gt;::merge(const LinkedList&lt;T&gt;&amp; other) const {
  
    
  LinkedList&lt;T&gt; L = *this;
    LinkedList&lt;T&gt; R = other;
  
    LinkedList&lt;T&gt; merged;
  
    if (L.head_ == nullptr) {
      return R;
    } else if (R.head_ == nullptr) {
      return L;
    } 
    
    
    while(L.head_ &amp;&amp; R.head_) {
      if (L.head_-&gt;data &lt; R.head_-&gt;data) {
        merged.pushBack(L.head_-&gt;data);
        L.head_ = L.head_-&gt;next;
        ++merged.size_;
      } else {
        merged.pushBack(R.head_-&gt;data);
        R.head_ = R.head_-&gt;next;
        ++merged.size_;
      }
    }
    
    while (L.head_ || R.head_) {
    if (L.head_) {
      merged.pushBack(L.head_-&gt;data);
      L.head_ = L.head_-&gt;next;
      ++merged.size_;
    } else {
      merged.pushBack(R.head_-&gt;data);
      R.head_ = R.head_-&gt;next;
      ++merged.size_;
    }
    }
  
  return merged;
    
  }
  </code></pre>
  <p>Does anyone know what's wrong with my code?</p>
  <p>This is the error I am getting</p>
  <pre><code>due to a fatal error condition:
    SIGABRT - Abort (abnormal termination) signal
  </code></pre>
  <p>This is the pushBack function I have for a doubly-linked list</p>
  <pre><code>// Push a copy of the new data item onto the back of the list.
  template &lt;typename T&gt;
  void LinkedList&lt;T&gt;::pushBack(const T&amp; newData) {
  
    // allocate a new node
    Node* newNode = new Node(newData);
  
    if (!head_) {
      // If empty, insert as the only item as both head and tail.
      // The Node already has next and prev set to nullptr by default.
      head_ = newNode;
      tail_ = newNode;
    }
    else {
      // Otherwise, add the new item as the tail.
      // (We could rewrite this without the temporary variable &quot;oldTail&quot;,
      //  but perhaps this way is clearer.)
      Node* oldTail = tail_;
      oldTail-&gt;next = newNode;
      newNode-&gt;prev = oldTail;
      tail_ = newNode;
    }
  
    // update size
    size_++;
  }
  </code></pre>