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)
  • It would be helpful to know what line is 54 is in the source file. – John Sensebe Jan 12 '16 at 17:24
  • 3
    Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – fabian Jan 12 '16 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 Jan 12 '16 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 – fadhil suhendi Jan 12 '16 at 19:06

2 Answers2

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
Manisha
  • 136
  • 4
  • 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! – fadhil suhendi Jan 13 '16 at 14:08
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.

Douglas Gardim
  • 420
  • 1
  • 4
  • 11
  • 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 – fadhil suhendi Jan 12 '16 at 19:08
  • I'm still confused that in what case that the flow do not get into the while loop? – fadhil suhendi Jan 12 '16 at 19:17