title: Why does my reverse() method reverse both MyLinkedList objects?
body: <p>I'm making a Doubly-Linked-List class, but I'm stuck on this reverse() method. I want the reverse() method to make and return a new MyLinkedList that is a reverse of the old one without changing the old one.</p>
  
  <pre><code>public MyLinkedList&lt;AnyType&gt; reverse()
  {
     MyLinkedList&lt;AnyType&gt; list = this;
  
     for(int i=0,j=list.theSize-1; i &lt; list.theSize/2; i++, j--)
         list.swap(i,j);
  
     return list;
  }
  
  public void swap(int index1, int index2)
  {
          if(index1 == index2)
              return;
          else if(index1 &gt; theSize-1 || index2 &gt; theSize-1)
          {
              try{
                  throw new IndexOutOfBoundsException();
              }
              catch(IndexOutOfBoundsException e)
              {
                  System.out.println("IndexOutOfBoundsException\n");
                  return;
              }             
          }
          Node&lt;AnyType&gt; first;
          Node&lt;AnyType&gt; last;
          if(index1 &lt; index2)
          {
              first = this.getNode(index1);
              last = this.getNode(index2);
          }
          else
          {
              first = this.getNode(index2);
              last = this.getNode(index1);
          }
          if((index1 == 0 &amp;&amp; index2 == theSize -1) ||(index1 == theSize -1 &amp;&amp; index2 == 0))
          {   
              first.next.prev = last;
              last.prev.next = first;
  
              first.prev = last.prev;
              last.next = first.next;
  
              first.next = endMarker;
              endMarker.prev = first;
              last.prev = beginMarker;
              beginMarker.next = last;
          }
  
          else if(Math.abs(index1-index2) == 1)
          {
              first.next = last.next;
              last.prev = first.prev;
              first.prev = last;
              last.next = first;
              last.prev.next = last;
              first.next.prev = first;
          }
  
          else
          {
              Node&lt;AnyType&gt; tempNext = last.next;
              Node&lt;AnyType&gt; tempPrev = last.prev;
  
              last.next.prev = first;
              last.prev.next = first;
              first.prev.next = last;
              first.next.prev = last;
  
              last.next = first.next;
              last.prev = first.prev;
              first.next = tempNext;
              first.prev = tempPrev;
          }
  }
  public static void main( String [ ] args )
  {
      MyLinkedList&lt;Integer&gt; lst = new MyLinkedList&lt;&gt;( );
      MyLinkedList&lt;Integer&gt; lst2 = new MyLinkedList&lt;&gt;( );
  
      for( int i = 0; i &lt; 10; i++ )
              lst.add( i );
      System.out.println("Original List: " + lst);
  
      lst2 = lst.reverse();
      System.out.println("Original List: " + lst);
      System.out.println("Reversed List: " + lst2);
  }
  </code></pre>
  
  <p>Here's my output:</p>
  
  <pre><code>Original List: [ 0 1 2 3 4 5 6 7 8 9 ]
  Original List: [ 9 8 7 6 5 4 3 2 1 0 ]
  Reversed List: [ 9 8 7 6 5 4 3 2 1 0 ]
  </code></pre>
  
  <p>As you can see, the original list is getting reversed as well. Why is this happening?</p>