title: Custom linked list data printing backword
body: <p>I just created my custom node class and created a linked list, but when I print the data in my list the data is printed backward.  I thought I set the pointers right and adding new node at the beginning of the list.  But apparently the  compiler thinks otherwise. It took me a while to finally understand pointers but I guess I didn't understand as well as I thought. </p>
  
  <pre><code>public class Node {
  
  public String data;
  public String cat; 
  public Node next; 
  
  public Node (String data, String cat, Node next){
      this.data=data; 
      this.cat=cat; 
      this.next=next; 
  }
  
  public Node() {
  
  }
  public String toString()
  {
      return "Patient: " + data + "   Category: " +cat ; 
  }
  </code></pre>
  
  <p>}</p>
  
  <hr>
  
  <pre><code>public class Main {
  
  public static Node head; 
  public static void main (String args [])
  {
      add("P1" , "2"); 
      add("P2", "3"); 
      add("P3", "4"); 
      add("P4", "4"); 
  printList(); 
  }
  
  // add data to nodes
  
  public  static void add(String d, String c)
  {
      Node temp = null; 
  
      if (head == null)
      {
          head = new Node(d, c, null); 
      }
      else
      {
          temp=head; 
          head= new Node(d, c, temp); 
      }
  
  }
  // print node data
  
  public static void printList()
  {
      while (head != null)
      {
          System.out.println(head); 
          head=head.next;      
      }
  }
  </code></pre>
  
  <p>}</p>