title: Reverse generic LinkedList by using swap method
body: <pre><code>        public class SimpleLinkedList&lt;E&gt; {
  
              public Node&lt;E&gt; head;
  
              public int size;
  
              public void add(E e) {
                  ++this.size;
                  if (null == head) {
                      this.head = new Node();
                      head.val = e;
                  } else {
                      Node&lt;E&gt; newNode = new Node();
                      newNode.val = e;
                      newNode.next = head;
                      this.head = newNode;
                  }
              }
  
  public void swap(E val1, E val2) {
          if (val1.equals(val2)) {
              return;
          }
          Node prevX = null, curr1 = head;
          while (curr1 != null &amp;&amp; !curr1.val.equals(val1)) {
              prevX = curr1;
              curr1 = curr1.next;
          }
          Node prevY = null, curr2 = head;
          while (curr2 != null &amp;&amp; !curr2.val.equals(val2)) {
              prevY = curr2;
              curr2 = curr2.next;
          }
          if (curr1 == null || curr2 == null) {
              return;
          }
          if (prevX == null) {
              head = curr2;
          } else {
              prevX.next = curr2;
          }
          if (prevY == null) {
              head = curr1;
          } else {
              prevY.next = curr1;
          }
          Node temp = curr1.next;
          curr1.next = curr2.next;
          curr2.next = temp;
      }
  
         public void reverse() {
              Node&lt;E&gt; prev = null;
              Node&lt;E&gt; current = head;
              Node&lt;E&gt; next = null;
              while (current != null) {
                  next = current.next;
                  current.next = prev;
                  prev = current;
                  current = next;
              }
              head = prev;
          }
  
  
          public static class Node&lt;E&gt; {
              public Node&lt;E&gt; next;
              public E val;
          }
      }
  
  
      public class SimpleLinkedListTest {
  
        @Test
      public void testReverseMethod() {
          SimpleLinkedList&lt;Integer&gt; myList = new SimpleLinkedList&lt;&gt;();
          for (int i = 0; i &lt; 10; i++) {
              myList.add(i);
          }
          SimpleLinkedList&lt;Integer&gt; expectedList = new SimpleLinkedList&lt;&gt;();
          for (int i = 9; i &gt; -1; i--) {
              expectedList.add(i);
          }
          myList.reverse();
          assertTrue(AssertCustom.assertSLLEquals(expectedList, myList));
      }
    }
  </code></pre>
  
  <p>What would be the most optimal way to reverse generic LinkedList by using the swap method? 
  before reverse method :</p>
  
  <p>(head=[9])->[8]->[7]->[6]->[5]->[4]->[3]->[2]->[1]->[0]-> null</p>
  
  <p>after reverse() method : </p>
  
  <p>(head=[0])->[1]->[2]->[3]->[4]->[5]->[6]->[7]->[8]->[9]-> null</p>