title: Reversing a singly linked list in java
body: <p>I made a singly linked list from scratch in java. The code is as follows: </p>
  
  <pre><code>public class SingleLinkedList&lt;Item&gt;
  {
  private Node head;
  private int size;
  
  private class Node
  {
      Item data;
      Node next;
  
      public Node(Item data)
      {
          this.data = data;
          this.next = null;
      }
  
      public Node(Item data, Node next)
      {
          this.data = data;
          this.next = next;
      }
      //Getters and setters
      public Item getData()
      {
          return data;
      }
      public void setData(Item data)
      {
          this.data = data;
      }
      public Node getNext()
      {
          return next;
      }
      public void setNext(Node next)
      {
          this.next = next;
      }
  
  }
  
  public SingleLinkedList()
  {
      head = new Node(null);
      size = 0;
  }
  
  public void add(Item data)
  {
      Node temp = new Node(data);
      Node current = head;
  
      while(current.getNext() != null)
      {
          current = current.getNext();
      }
  
      current.setNext(temp);
      size++;
  }
  
  public void add(Item data, int index)
  {
      Node temp = new Node(data);
      Node current = head;
  
      for(int i=0; i&lt;index &amp;&amp; current.getNext() != null; i++)
      {
          current = current.getNext();
      }
  
      temp.setNext(current.getNext());
      current.setNext(temp);
      size++;
  }
  
  public Item get(int index)
  {
      if(index &lt;= 0)
      {
          return null;
      }
  
      Node current = head;
  
      for(int i=1; i&lt;index; i++)
      {
          if(current.getNext() == null)
          {
              return null;
          }
  
          current = current.getNext();
      }
  
      return current.getData();
  }
  
  public boolean remove(int index)
  {
      if(index &lt; 1 || index &gt; size())
      {
          return false;
      }
  
      Node current = head;
      for(int i=1; i&lt;index; i++)
      {
          if(current.getNext() == null)
          {
              return false;
          }
  
          current = current.getNext();
      }
  
      current.setNext(current.getNext().getNext());
      size--;
      return true;
  }
  
  public String toString()
  {
      Node current = head.getNext();
      String output = "";
      while(current != null)
      {
          output+=current.getData().toString()+"  ";
          current = current.getNext();
      }
  
      return output;
  }
  
  public int size()
  {
      return size;
  }
  
  public void reverse()
  {
      Node current = head;
      Node prevNode = null;
      Node nextNode;
  
      while(current!=null)
      {
          nextNode = current.getNext();
          current.setNext(prevNode);
          prevNode = current;
          current = nextNode;
          System.out.println(prevNode.getData());
      }
  
      head = prevNode;
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>As you can see, I added the reverse function in the class only.</p>
  
  <p>But when I tried actually using the class it gave NullPointerException after I tried to reverse it.</p>
  
  <p>To check the functionality I used another class called TEST. The code is as follows:</p>
  
  <pre><code>public class TEST
  {
  
      public static void main(String[] args)
      {
          SingleLinkedList&lt;Integer&gt; list = new SingleLinkedList&lt;Integer&gt;();
  
          list.add(1);
          list.add(2);
          list.add(3);
          list.add(4);
          list.add(5);
  
          System.out.println(list.toString());
  
          list.reverse();
          System.out.println(list.toString());
  
      }
  }
  </code></pre>
  
  <p>The output is as follows: </p>
  
  <pre><code>1  2  3  4  5  
  null
  1
  2
  3
  4
  5
  Exception in thread "main" java.lang.NullPointerException
      at SingleLinkedList.toString(SingleLinkedList.java:129)
      at TEST.main(TEST.java:20)
  </code></pre>
  
  <p>I tried to print the value of prevNode to check whether its not taking values...but it is.
  What to do?</p>