I have written this code to insert a new node at any position of the "Linked List"
I know there is build-in library in Java; I am writing this as a practice for Java.
But I get error
java.lang.NullPointerException
I don't know where the problem. Here is my code:
public void insertAtPos(int val , int pos) {
Node nptr = new Node(val, null);
Node current = start;
/* Crawl to the requested index */
for (int i = 0; i < pos; i++){
current = current.getLink();
}
/* Set the new node's next-node reference to this node's next-node reference */
nptr.setLink(current.getLink());
/* Set the new node's next-node reference to the new node */
current.setLink(nptr);
size++ ;
}
/* Function to set link to next Node */
public void setLink(Node n) {
link = n;
}
/* Function to get link to next node */
public Node getLink() {
return link;
}