Cloning a singly linked list using overloaded assignment operator
<p>I'm trying to implement a Singly linked list class in C++.  I've overloaded the assignment operator to clone the list.  The cloning itself seems to work fine, but the program crashes during deletion of the cloned list, which leads me to suspect if something's wrong during the copy procedure.  </p>

<p>Any help is highly appreciated.
Here's the code for the overloaded = operator:</p>

<pre><code>DLList&amp; DLList::operator=(const DLList&amp; iList)
{
    Node *t = iList.head();
    while(t != NULL)
    {
        push(t-&gt;data);
        t = t-&gt;next;
    }
    return *this;
}
</code></pre>

<p>Here's the code for the push operation:</p>

<pre><code>void DLList::push(int data)
{
    Node *n = new Node;
    n-&gt;data = data;
    n-&gt;next = NULL;
    //n-&gt;random = NULL;
    n-&gt;next = _head;
    _head = n;
    //cout &lt;&lt; "_head" &lt;&lt;_head-&gt;data&lt;&lt; "head-&gt;next" &lt;&lt; (_head-&gt;next ? _head-&gt;next-&gt;data : 0)&lt;&lt;endl;
}
</code></pre>

<p>Here's the main code (where the crash happens) :</p>

<pre><code>DLList *d = new DLList();
d-&gt;push(10);
d-&gt;push(20);
d-&gt;push(30);
d-&gt;push(40);
d-&gt;setRandom();
d-&gt;display("Original list");  // Displays the original list fine
DLList *d1 = new DLList();
d1 = d;  
d1-&gt;display("Cloned list"); //Displays the cloned list fine
delete d;    // works fine
delete d1;  // Crashes here due to segmentation fault, invalid pointer access during delete called as part of destructor)
</code></pre>

<p>Code for destructor (if it helps):</p>

<pre><code>~DLList()
{
    while(_head != NULL)
    {
        Node *temp = _head;
        _head = _head-&gt;next;
        delete temp;  // crashes here during first iteration for the cloned list
    }
}
</code></pre>