使用java在特定位置插入节点

时间:2020-12-05 01:39:02

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:

我正在针对在特定位置插入节点的黑客级别做问题。我在这种情况下使用java,但我一直收到错误。我不知道如何解决它。我感谢您的帮助。这是我的解决方案:

/*`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)

2 个解决方案

#1


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

在您给出的代码片段中,您在评论中提到第一个元素位于位置0.因此,如果插入发生在位置0,那么head将会改变。因此你所处的条件

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

哟实际应该检查位置== 0.而你所说的输出中的不间断重复只是因为这个。例如,如果链接列表10-> 20,我希望在位置0插入30,然后我们按你的代码然后我们不会进入循环为0(计数器)!= -1(位置-1)所以我们prevNode和currNode他们现在指向10和

        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

#2


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.

就我在你发送的信息中看到的那样,如果流不进入“while”循环,则会发生NullPointerException,因此“prevNode”将保持为null。然后,您将在“prevNode.next = nNode”行中获得异常。

It will be easy to get if you debug the code.

如果您调试代码将很容易获得。

#1


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

在您给出的代码片段中,您在评论中提到第一个元素位于位置0.因此,如果插入发生在位置0,那么head将会改变。因此你所处的条件

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

哟实际应该检查位置== 0.而你所说的输出中的不间断重复只是因为这个。例如,如果链接列表10-> 20,我希望在位置0插入30,然后我们按你的代码然后我们不会进入循环为0(计数器)!= -1(位置-1)所以我们prevNode和currNode他们现在指向10和

        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

#2


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.

就我在你发送的信息中看到的那样,如果流不进入“while”循环,则会发生NullPointerException,因此“prevNode”将保持为null。然后,您将在“prevNode.next = nNode”行中获得异常。

It will be easy to get if you debug the code.

如果您调试代码将很容易获得。