title: Program to reverse a linked list in Java
body: <p>I have written the following code, in which method <code>rev(list,list)</code> is not working.  Please help me determine what's wrong.</p>
  
  <pre><code>import java.io.*;
  public class list
  {
      int d;
      list l;
      list()
      {
          d=0;
          l=null;
      }
  
      void create()throws IOException
      {
          int n;// store number of nodes
          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
          System.out.println("Enter the first data");
          this.d=Integer.parseInt(br.readLine());
          System.out.println("Enter number of nodes to be made");
          n=Integer.parseInt(br.readLine());
          list temp;
          list ptr=this;
          for(int i=1;i&lt;n;i++)
          {
              temp=new list();
              System.out.println("Enter the next data");
              temp.d=Integer.parseInt(br.readLine());
              temp.l=null;
              ptr.l=temp;
              temp=null;
              ptr=ptr.l;
          }
      }
  
      void delete(list lst, int n)
      {
          list ptr=lst;
          list ptr1=ptr;
          int c=1;
          while(c&lt;n)
          {
              ptr1=ptr;
              ptr=ptr.l;
              c++;
          }
          ptr1.l=ptr.l;
          ptr.l=null;
          ptr=null;
          ptr1=null;
      }
  
      void insertmid(list lst,int x, int n)
      {
          list temp=new list();
          temp.d=x;
          temp.l=null;
          list ptr=lst;
          int c=1;
          while(c&lt;n)
          {
              ptr=ptr.l;
              c++;
          }
          temp.l=ptr.l;
          ptr.l=temp;
      }
  
      void rev(list lst,list lst1)
      {
  
          lst1=null;
          list ptr=new list();
  
          while(lst!=null)
          {
              ptr =lst;
              lst=lst.l;
              ptr.l=lst1;
              lst1=ptr;
  
          }
      }
  
  
      void display()
      {
          list ptr=this;
  
          while(ptr!=null)
          {
              System.out.print(ptr.d+"\t");
              ptr=ptr.l;
          }
          System.out.println();
      }
  
      public static void main(String args[])throws IOException
      {
          list l2=new list();
          list l3=new list();
  
          l2.create();
          l2.display();
          l2.insertmid(l2,14,2);
          l2.display();
          l2.delete(l2, 3);
          l2.display();
          l2.rev(l2,l3);
          l2.display();
  
      }
  }
  </code></pre>