title: Exception thrown: write access violation. temp was nullptr
body: <p>I am getting an error in the <code>insert()</code> function in the <code>else</code> statement.</p>
  
  <p>Here is the struct:   </p>
  
  <pre class="lang-cpp prettyprint-override"><code>struct Node
  {
      int data;
      Node* next;
  };
  Node* head = NULL;
  </code></pre>
  
  <p>and here is the function:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>void insert(int data)
  {
      Node* New = new Node();
      if (head == NULL)
      {
          New-&gt;data = data;
          head = New;
      }
      else
      {
          // down here where the error occured
          Node* temp = new Node();
          temp = head;
          while(temp != NULL)
          {
              temp = temp-&gt;next;
          }
          temp-&gt;data = data;
          temp-&gt;next = New;
      }
  }
  </code></pre>