title: How to reverse singly linked list - Java
body: <p>Hi I am wondering how to go about reversing a singly linked list. From the examples I've looked at so far, the reverse methods have void as their return type and I need one that has singly linked list (SLL) return type. I have an inner class called Node to deal with all the node references.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>public SLL&lt;E&gt; reverse() {
      Node&lt;E&gt; first = null;
      Node&lt;E&gt; current = this;  // produces compilation error
      while (current != null) {
          Node&lt;E&gt; save = current;
          current = current.next;
          save.next = first;
          first = save;
      }
      return first;
  }
  </code></pre>
  
  <p>I get the following compilation error:</p>
  
  <p>Error: incompatible types
    required: myprog.SLL.Node
    found:    myprog.SLL</p>
  
  <p>I am just wondering how I would return the list with type SLL when dealing with Node. I also think my return statement might be wrong.</p>
  
  <p>If you need more code to understand my question, just ask :)</p>
  
  <p>Thanks in advance!</p>