title: Linked list copy constructor C++
body: <p>I am really confused here as to why this copy constructor is not working! I am creating an <code>iter</code> pointer that points to the same ListNode as <code>head</code>, but when I copy stuff from <code>s</code> to <code>it</code>, <code>head</code> and <code>iter</code> are not connected!</p>
  
  <p>In other words when printing head, only the first character is in there, but if I were to iterate through <code>iter</code>, the rest of the list is in there.
  Why isn't <code>iter</code> and <code>head</code> pointing to the same objects?!</p>
  
  <p>NOTE: This is a linked list being used to implement a class called MyString.</p>
  
  <pre><code>struct ListNode {
      char info;
      ListNode *next;
      ListNode () : info('0'), next(0) {}
      ListNode (char c) : info (c), next(0) {}
  };
  
  class MyString {
      private:
      ListNode *head;
  
      MyString::MyString(const MyString &amp; s) {
          if (s.head == 0)
              head = 0;
          else {
              head = new ListNode (s.head -&gt; info);
              ++NumAllocations;
              ListNode *iter = head;
              for (ListNode *ptr = s.head -&gt; next; ptr != 0; ptr = ptr -&gt;next) {
                  iter = iter -&gt; next;
                  iter = new ListNode (ptr -&gt; info);
                  ++NumAllocations;
              }
          }
      }
  }
  </code></pre>