title: A proper iteration of all LinkedList nodes in Javascript
body: <p>In my code snippet I am iterating of LinkedList nodes using a while loop, I am console.logging each node value. My while loop exists and my last value has to be console.logged next line after the while loop, anyway to make a more elegant iterator for my LinkedList?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>function LinkedList() {
      this.head = null;
  };
  
  LinkedList.prototype = (function () {
      function reverseAll(current, prev) {
          if (!current.next) { //we have the head
              this.head = current;
              this.head.next = prev;
          }
  
          var next = current.next;
          current.next = prev;
  
          reverseAll(next, current);
      };
  
      return {
          constructor: LinkedList,
  
          reverse: function () {
              reverseAll(this.head, null);
          },
  
          head: function() {
              return this.head;
          }
      }
  })();
  
  LinkedList.prototype.add = function(value) {
      var node = {
          value: value,
          next: null
      };
  
      var current;
  
      if (this.head === null) {
          this.head = node;
      } else {
          current = this.head;
          while (current.next) {
              current = current.next;
          }
          current.next = node;
      }
  
      return node;
  }
  
  LinkedList.prototype.remove = function(node) {
      var current, value = node.value;
  
      if (this.head !== null) {
          if (this.head === node) {
              this.head = this.head.next;
              node.next = null;
              return value;
          }
          //find node if node not head
          current = this.head;
          while (current.next) {
              if (current.next === node) {
                  current.next = node.next;
                  return value;
              }
  
              current = current.next;
          }
      }
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script&gt;
  $(function() {
      var obj = new LinkedList();
      
      for (var i = 1; i &lt;= 10; i++) {
          obj.add(i);
      }
  
      var current = JSON.parse(JSON.stringify(obj.head));
  
      while (current.next) {
          console.log(current.value);
  
          current = current.next;
  
      }
      //not so hot iteration, printing last value that would be great if printed in the while loop
      console.log(current.value);
  });
  &lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>