title: Segmentation fault in LinkedList destructor
body: <pre><code>LinkedList::~LinkedList()
  {
      Node *ptr = head;
      while (ptr-&gt;getNext() != nullptr) {
          Node *hold = ptr;
          ptr = ptr-&gt;getNext();
          delete hold;
      }
  }
  </code></pre>
  
  <p>I'm getting a segmentation fault at the while loop's condition when instantiating an empty LinkedList object. I'll add the Node and LinkedList class cpp files below as well.</p>
  
  <h1>LinkedList.cpp</h1>
  
  <pre><code>#include "LinkedList.h"
  
  LinkedList::LinkedList()
  {
      head = nullptr;
  }
  
  /** Make sure to dereference Node pointer when returning **/
  Node* LinkedList::getNode(int e) {
      Node *ptr = head;
      while (ptr-&gt;getNext()) {
          if (ptr-&gt;getExponent() == e) {
              return ptr;
          }
          ptr = ptr-&gt;getNext();
      }
      return nullptr;
  }
  
  void LinkedList::addNode(int c, string b, int e) {
      Node *n = nullptr;
      *n = Node(c, b, e);
      if (!head) {
          head = n;
          return;
      }
      else if (n-&gt;getExponent() &lt; n-&gt;getExponent()) {
          n-&gt;setNext(head);
          head = n;
      }
      else {
          Node *ptr = head;
          while (ptr-&gt;getNext()) {
              if (ptr-&gt;getNext()-&gt;getExponent() &gt; n-&gt;getExponent()) {
                  n-&gt;setNext(ptr-&gt;getNext());
                  ptr-&gt;setNext(n);
                  return;
              }
              ptr = ptr-&gt;getNext();
          }
          ptr-&gt;setNext(n);
      }
  }
  
  void LinkedList::deleteNode(int e) {
      if (!head) {
          return;
      }
      else if (head-&gt;getExponent() == e) {
          Node *hold = head;
          head = head-&gt;getNext();
          delete hold;
      }
      else {
          Node *ptr = head;
          while (ptr-&gt;getNext()) {
              if (ptr-&gt;getNext()-&gt;getExponent() == e) {
                  Node *hold = ptr-&gt;getNext();
                  ptr-&gt;setNext(ptr-&gt;getNext()-&gt;getNext());
                  delete hold;
                  return;
              }
              ptr = ptr-&gt;getNext();
          }
      }
  }
  
  string LinkedList::toString() {
      string line = "";
      Node *ptr = head;
      while (ptr-&gt;getNext()) {
          line += ptr-&gt;getTerm() + "";
          ptr = ptr-&gt;getNext();
      }
      return line;
  }
  
  void LinkedList::derive() {
  
  }
  
  LinkedList::~LinkedList()
  {
      Node *ptr = head;
      while (ptr-&gt;getNext() != nullptr) {
          Node *hold = ptr;
          ptr = ptr-&gt;getNext();
          delete hold;
      }
  }
  </code></pre>
  
  <h1>Node.cpp</h1>
  
  <pre><code>#include "Node.h"
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  
  using namespace std;
  
  Node::Node() {
      coefficient = 0;
      base = "";
      exponent = 0;
      next = nullptr;
  }
  
  Node::Node(int c, string b, int e) {
      coefficient = c;
      base = b;
      exponent = e;
      next = nullptr;
  }
  
  string Node::getTerm() {
      stringstream term;
      term &lt;&lt; coefficient &lt;&lt; base &lt;&lt; "^" &lt;&lt; exponent;
      return term.str();;
  }
  
  Node::~Node() {}
  </code></pre>