I'm doing the problem on hacker rank on inserting node at specific position. Im using java in this case, but I keep getting an error. And I don't know how to fix it. I appreciate your help. Here is my solution:
/*`enter code here`
Insert Node at a given position in a linked list
head can be NULL
First element in the linked list is at position 0
Node is defined as
class Node {
int data;
Node next;
}*/
Node InsertNth(Node head, int data, int position) {
`enter code here`// This is a "method-only" submission.
// You only need to complete this method.
if(head == null){
Node newNode = new Node();
newNode.data = data;
newNode.next = null;
return head;
}
if(position == 1){
Node newNode = new Node();
newNode.data = data;
newNode.next = head;
head = newNode;
return head;
}
// we need to go to n - 1
int counter = 0;
Node currNode = head;
Node prevNode = null;
while(counter != position -1 && currNode.next != null){
prevNode = currNode;
currNode = currNode.next;
counter++;
}
Node nNode = new Node();
nNode.data = data;
prevNode.next = nNode;
nNode.next = currNode;
return head;
/* another solution */
}
Result:
Exception in thread "main" java.lang.NullPointerException
at Node.InsertNth(Solution.java:54)
at Solution.main(Solution.java:89)
-
It would be helpful to know what line is 54 is in the source file.John Sensebe– John Sensebe2016年01月12日 17:24:36 +00:00Commented Jan 12, 2016 at 17:24
-
3Possible duplicate of What is a Null Pointer Exception, and how do I fix it?fabian– fabian2016年01月12日 17:26:03 +00:00Commented Jan 12, 2016 at 17:26
-
I see several major problems with this code. You need to carefully reread the problem and go through the code line by line.John Sensebe– John Sensebe2016年01月12日 17:35:21 +00:00Commented Jan 12, 2016 at 17:35
-
Thanks for the replay! I tried to fix the code from Node prevNode = null become Node prevNode = head. and now the error for the NullPointerException is gone. Instead I got the repeating number nonstop for the output. So I guess I need to fix the loop conditionfadhil suhendi– fadhil suhendi2016年01月12日 19:06:28 +00:00Commented Jan 12, 2016 at 19:06
2 Answers 2
In the code snippet that you have given, in comment you have mentioned that first element is at position 0. So in case insertion happens at position 0 then head will change. Thus the condition where you do
if(position == 1){
Node newNode = new Node();
newNode.data = data;
newNode.next = head;
head = newNode;
return head;
}
Yo should actually check position == 0. And the non stop repetition in your output that you are saying is because of this only. E.g if linked list 10->20 , I wish t insert 30 at position 0 , and we go by your code then we will not enter the loop as 0(counter) != -1 (position -1) so we prevNode and currNode both are pointing to 10 now and
Node nNode = new Node();
nNode.data = data;
prevNode.next = nNode; // you made 10 point to 30
nNode.next = currNode; // here you made 30 point to 10 so **loop** here
1 Comment
As far as I can see in the information you sent, the NullPointerException will happen if the flow don't get into the "while" loop, so the "prevNode" will remain null. Then you will get the exception in the "prevNode.next = nNode" line.
It will be easy to get if you debug the code.