title: Reversing a linked list in Java, recursively
body: <p>I have been working on a Java project for a class for a while now. It is an implementation of a linked list (here called <code>AddressList</code>, containing simple nodes called <code>ListNode</code>). The catch is that everything would have to be done with recursive algorithms. I was able to do everything fine sans one method: <code>public AddressList reverse()</code>    </p>
  
  <p>ListNode:  </p>
  
  <pre><code>public class ListNode{
    public String data;
    public ListNode next;
  }
  </code></pre>
  
  <p>Right now my <code>reverse</code> function just calls a helper function that takes an argument to allow recursion.</p>
  
  <pre><code>public AddressList reverse(){
    return new AddressList(this.reverse(this.head));
  }
  </code></pre>
  
  <p>With my helper function having the signature of <code>private ListNode reverse(ListNode current)</code>.</p>
  
  <p>At the moment, I have it working iteratively using a stack, but this is not what the specification requires. I had found an algorithm in C that recursively reversed and converted it to Java code by hand, and it worked, but I had no understanding of it.</p>
  
  <p>Edit: Nevermind, I figured it out in the meantime.</p>
  
  <pre><code>private AddressList reverse(ListNode current, AddressList reversedList){
    if(current == null) 
        return reversedList;
    reversedList.addToFront(current.getData());
    return this.reverse(current.getNext(), reversedList);
  }
  </code></pre>
  
  <p>While I'm here, does anyone see any problems with this route?</p>