title: iteratively deep copy a link list
body: <p>This is an homework assignment. To change the following recursive deep copy method into an iterative equivalent. I came up close, and need your help to make it right.
  Recursive implementation:</p>
  
  <pre><code>public static StringNode copy(StringNode str) {
          if (str == null)
              return null;
  
          StringNode copyFirst = new StringNode(str.ch, null);
          copyFirst.next = copy(str.next);
          return copyFirst;
      }
  </code></pre>
  
  <p>Here is what I came up, the iterative equivalent. The <code>static length()</code> method has been implemented already to return how many nodes are there in a given link list.</p>
  
  <pre><code>public static StringNode copy(StringNode str) {
      if (str == null)
          return null;
  
      StringNode firstNode = new StringNode(str.ch ,null);
      StringNode prevNode = firstNode;
      StringNode nextNode;
  
      for (int i = 1; i &lt; length(str); i++) {
          nextNode = new StringNode(str.next.ch, null);
          prevNode.next = nextNode;
          prevNode = nextNode;
      }
  
      return firstNode;
  }
  </code></pre>
  
  <p>The problem: to test my implementation, I create a linked list <code>str1</code> with character value, <code>'n', 'b', 'a'</code>, then call </p>
  
  <pre><code>StringNode copy = StringNode.copy(str1);
  </code></pre>
  
  <p>then I delete the last node of str1, leave it as <code>'n','b',</code>
  however, when i try to print out the content stored in copy, I get
  <code>'n', 'b', 'b'</code> instead of <code>'n', 'b', 'a'</code>.</p>
  
  <p>Any suggestions?</p>