title: Linked List Insertion in the middle
body: <pre><code>Node InsertNth(Node head, int data, int position) {
  
  
      Node start,curr,temp;
      start=head;
      curr=start;
      if(start==null)
          {
          temp=new Node();
          temp.data=data;
          temp.next=null;
          return head;
      }
      else if(position==0)
          {
          temp=new Node();
          temp.data=data;
          temp.next=start;
          return head;
      }
      else
          {
          for(int i=0;i&lt;position;i++)
              {
              System.out.println("i:"+i);
              curr=start;
              start=start.next;
          }
          temp=new Node();
          temp.data=data;
          curr.next=temp;
          temp.next=start;
          return head;
      } 
  }
  </code></pre>
  
  <p>In the above code I printed the value of "i" in the for-loop.
  In the console I am getting the output as </p>
  
  <pre><code>i:0
  i:0
  i:1
  i:0
  i:1
  i:2
  i:3
  </code></pre>
  
  <p>And </p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at Node.InsertNth(Solution.java:49)
      at Solution.main(Solution.java:88)
  </code></pre>
  
  <p>Why is "i" not incrementing properly? If it does work well then I can perform insertion in middle.</p>