0

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)
asked Jan 12, 2016 at 17:17
4
  • It would be helpful to know what line is 54 is in the source file. Commented Jan 12, 2016 at 17:24
  • 3
    Possible duplicate of What is a Null Pointer Exception, and how do I fix it? Commented 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. Commented 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 condition Commented Jan 12, 2016 at 19:06

2 Answers 2

1

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
answered Jan 13, 2016 at 10:48

1 Comment

Hi, thank you for your replay! It helped me a lot! I tried to change the condition to be if(position == 0) then execute the code inside it. Then I change the loop from (position - 1) to be just position. Now the code works!
0

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.

answered Jan 12, 2016 at 17:37

2 Comments

Hi thanks for the replay! yes, you are right. When I change the code to prevNode = head the NullPointerException is gone. Now I have the repeating number nonstop for the output. I'm trying to fix the loop condition now
I'm still confused that in what case that the flow do not get into the while loop?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.