title: Override = operator linked linked c++ deep copy
body: <p>So, I'm trying override operator= in a linked list class I'm writing, but keep getting this weird problem for some reason. </p>
  
  <pre><code>List&amp; List::operator=(const List&amp; copyList){
  if(copyList.head != nullptr){
      makeEmpty();   // clears *this from any previous nodes
      cout &lt;&lt; "if statement " &lt;&lt; endl;
      head = new Node; // create a new node for head
      head -&gt; data = copyList.head -&gt; data; // copy the first data of copylist
      Node* pnew = head; // a temp node to traverse the new linkedlist
      assert(head != nullptr);
      Node* current2 = copyList.head;
      current2 = current2 -&gt; next;
      while(current2 != NULL &amp;&amp; pnew != NULL){
          cout &lt;&lt; "entering while loop " &lt;&lt; endl;
          pnew-&gt; next = new Node;
          pnew -&gt; next-&gt;data = current2 -&gt;data;
          cout &lt;&lt; "pnew next data " &lt;&lt; *(pnew -&gt; next-&gt;data)  &lt;&lt; endl;
          assert(pnew-&gt; next != nullptr);
          pnew = pnew -&gt; next;
          current2 = current2 -&gt; next;
          cout &lt;&lt; "after current2" &lt;&lt; endl;
      }
       pnew -&gt; next = NULL;
  
  }else{
      cout &lt;&lt; "else statement " &lt;&lt; endl;
      head = nullptr;
  }
  cout&lt;&lt; "printing out copylist"&lt;&lt; endl &lt;&lt; copyList &lt;&lt; endl;
  cout&lt;&lt; "printing current list: " &lt;&lt; endl &lt;&lt; *this &lt;&lt; endl;
  return *this;
  </code></pre>
  
  <p>}</p>
  
  <p>So, this is the code I have to test the operator override:</p>
  
  <pre><code>cout &lt;&lt; "mylist:" &lt;&lt; endl &lt;&lt; mylist &lt;&lt; endl;
     cout &lt;&lt; "mylist4:" &lt;&lt; endl &lt;&lt; mylist4 &lt;&lt; endl;
     mylist = mylist4;
     cout &lt;&lt; "mylist:" &lt;&lt; endl;
     cout &lt;&lt; mylist &lt;&lt; endl;
     cout &lt;&lt; "mylist4:" &lt;&lt; endl;
     cout &lt;&lt; mylist4 &lt;&lt; endl;
  </code></pre>
  
  <p>This is the output:</p>
  
  <pre><code>mylist:
  10 f
  16 u
  20 n
  25 !
  
  mylist4:
  14 s
  15 t
  16 u
  18 f
  19 f
  25 !
  
  if statement
  entering while loop
  pnew next data 15 t
  
  after current2
  entering while loop
  pnew next data 16 u
  
  after current2
  entering while loop
  pnew next data 18 f
  
  after current2
  entering while loop
  pnew next data 19 f
  
  after current2
  entering while loop
  pnew next data 25 !
  
  after current2
  printing out copylist
  14 s
  15 t
  16 u
  18 f
  19 f
  25 !
  
  printing current list:
  14 s
  15 t
  16 u
  18 f
  19 f
  25 !
  *crashes right here*
  </code></pre>
  
  <p>I have been trying to figure out this problem for about 3 days. Any help would be highly appreciated. Thanks in advance!   </p>
  
  <p>EDIT: here are the constructor (the destructor is the default one by the compiler):</p>
  
  <pre><code>NodeData::NodeData(int n, char c)  { 
      num = n; ch = c; 
  } 
  </code></pre>
  
  <p>EDIT2: I found the problem after careful examination. The problem was that I did not point the last node of head, pnew after the while loop, to null. This fixed the issue. Thanks to all your support. </p>