title: How to insert node at nth position in linked list
body: <p>I am beginner in programming. I am trying to implement linked list in java i have tried to write function to insert element at nth position but its not working properly its not showing data before that position. It may seem silly question or mistake to you but as i am beginner so your answer will be helpful and it will be appreciated.</p>
  
  <p>Thanks in advanced.</p>
  
  <p>The Code is below.</p>
  
  <pre><code>class Node{
  int data;
  Node next;
  Node(){
      data=0;
      next=null;
  }
  }
  
  class LinkedList{
  Node head;
  LinkedList(){
      head=null;
  }
  
     void pushB(int item){
         Node temp=new Node();
         temp.data=item;
         temp.next=null;
          if(head==null){
          head=temp;    
          }
          else{
              temp.next=head;
              head=temp;
          }
      }
  
      void pushnth(int item, int pos){
  
       Node cur=new Node();
       cur.data=item;
       cur.next=null;
       Node temp=head;
       int i=0;
  
       while(i&lt;pos-1){
           temp=temp.next;
           i++;
       }
       cur.next=temp;
       head=cur;
      }
  
      void print(){
          if(head==null){
              System.out.println("List empty");
          }
  
          else{
              Node temp=head;
              while(temp!=null){
                  System.out.println(temp.data);
                  temp=temp.next;
              }
          }
      }
  }
  
  
  public class MyFirstJavaProgram {
  
  public static void main(String []args) {
     System.out.println("Hello World");
  
     LinkedList l1=new LinkedList();
  
     l1.pushB(90);
     l1.pushB(80);
     l1.pushB(70);
     l1.pushB(60);
     l1.pushB(50);
     l1.pushB(30);
     l1.pushB(20);
     l1.pushB(10);
     l1.pushnth(40,4);
     l1.print();
  }
  } 
  </code></pre>