title: How do I remove the only node in a linked list in java?
body: <p>I have a while loop that is supposed to run until a certain value is deleted from my linked list.(remainingPoints) The problem is that my compiler gives me an error if that node is the only node in the linked list. edit: I'm using the prewritten linked list class in java.</p>
  
  <pre><code>    while (remainingPoints.contains(endPoint)) {
  
          //loop through each index of the start points adjacency array and update the path estimates.
          for (int i = 0; i &lt; maxIndex; i++) {
              //if the path estimate is greater than the distance found from the next Point to the
              //i'th point, update the pathEstimate for that point and update the parent of the point.
              if ((pathEstimates[i] != 0 &amp;&amp; adjMatrix[next][i] != 0) &amp;&amp; (adjMatrix[next][i]+pathEstimates[next] &lt; pathEstimates[i])) {
                  pathEstimates[i] = adjMatrix[next][i] + pathEstimates[next];
                  parents[i] = next;
              }
          }
  
          //reset next. 
          next = -1;
  
          //This will be the intersection that has the shortest path from the last tested 
          //intersection (that is not 0).
          for (int i = 0; i &lt; maxIndex; i++) {
              if (pathEstimates[i] != 0 &amp;&amp; remainingPoints.contains(i)) {
                  if (next == -1)
                      next = i;
                  else if (pathEstimates[i] &lt; next)
                      next = i;
              }
          }
  
          //Inelegent solution goes here in place of the line of code below:
          remainingPoints.remove(next);
  
      }
  </code></pre>
  
  <p>An inelegant solution I tried was to add in this if statement which added a useless node containing -1 so that the node containing next could be deleted making the next iteration of the while loop false, but this added a more curious problem:</p>
  
  <pre><code>    if (remainingPoints.size() == 1)
              remainingPoints.add(-1);
      System.out.println(next);
      System.out.println(remainingPoints.remove(next));
  </code></pre>
  
  <p>With this attempted solution the loop runs infinitely. The value of next that is printed is 1(this is the correct and intended value of next) but somehow the value of the remainingPoints.remove(next) is -1. I tested other values as well and the value of remainingPoints.remove(next) is always the value that I add using the if statement. This suggests that the the remove method is removing the value I added in which would explain the infinite loop but why is this happening?</p>
  
  <p>If anyone could explain how to simply delete the only node in a linked list in java it would be much appreciated!! A bonus for whoever can explain the above error as well.</p>
  
  <p>As an aside, this is my first post on stack overflow so if I made any posting errors or am ignorant to any stack overflow etiquette please let me know!</p>