title: Remove all linked list in Java
body: <p>I'm learning Java &amp; Data structures.</p>
  
  <p>I made a method that removes all the linked list. Here is the code:</p>
  
  <pre><code>public class LinkedList {
      ...
      public void purge() {
          SLLNode nextNode, iterator = headNode;
  
          while(iterator != null) {
              nextNode = iterator.getNext();
              iterator = null;
              iterator = nextNode;
          }
      }
      ...
  }
  </code></pre>
  
  <p>And here is the main method:</p>
  
  <pre><code>public class SinglyLinkedList {
      public static void main(String[] args) {
          SinglyLinkedList test = new SinglyLinkedList();
  
          test.insert(1, 0); // insert(data, position)
          test.insert(2, 1);
          test.insert(3, 2);
          test.insert(4, 1);
          test.purge();
          test.printList(); // Still prints elements!
      }
  }
  </code></pre>
  
  <p>So after all of insertion, the list is: 1-> 4 -> 2 -> 3, but the method purge() doesn't remove elements - the method printList() still prints the values in the linked list.  But I can purge the linked list like:</p>
  
  <pre><code>public void new_purge() {
      headNode = null;
  }
  </code></pre>
  
  <p>But the purge() method doesn't look like have a problem.. probably it's lack of Java understanding, but why the method purge() doesn't work here?</p>