【问题标题】:Why my code is throwing NullPointerException though it's fine with logic and also passes the sample testcases?为什么我的代码抛出 NullPointerException 虽然它的逻辑很好并且还通过了示例测试用例?
【发布时间】:2020-10-12 09:09:34
【问题描述】:

代码在 gfg 上:https://practice.geeksforgeeks.org/problems/swap-kth-node-from-beginning-and-kth-node-from-end-in-a-singly-linked-list/1

我们的任务是完成函数swapkthnode()

这个特定的代码有什么问题?它适用于对问题本身给出的示例测试用例/示例进行自定义检查。我已经在在线 IDE jdoodle 中测试了示例案例,它在那里运行良好。 但是在 gfg IDE 中编译和测试时,它在下面的注释行给出了 NullPointerException(相同逻辑的 c++ 中的分段错误);

Node swapkthnode(Node head, int num, int K)
{
    // your code here
    if(num == 1 || 2*K - 1 == num) return head;
    if(K == 1 || K == num)
    {
        Node start = head;
        Node end = head;
        
        while(end.next.next != null) end = end.next;
        
        head = end.next;
        head.next = start.next;
        end.next = start;
        start.next = null;
        return head;
    }
    else
    {
        Node lastK = head;
        Node firstK = head;
        
        for(int i=K-2; i>0; i--) firstK = firstK.next;
        for(int i=num-K-1; i>0; i--) lastK = lastK.next;
        
        Node tmp1 = firstK.next;
        Node tmp2 = lastK.next.next;

        firstK.next = lastK.next;
        lastK.next = tmp1;
        firstK.next.next = tmp1.next; // <<<===== May be This is the faulty line according to exception thrown, but I am unable to see any fault in there.
        tmp1.next = tmp2; // beacause if firstK.next is going to be null in any example,
        return head; // then it will be already handled by above 2nd if condition.
    }
}

堆栈跟踪:stackTrace

【问题讨论】:

  • 如果您怀疑该行,为什么不检查firstK.next 是否为空?您能否提供指示特定代码行的堆栈跟踪?
  • @AlexRudenko 看看stackTrace,我已经上传了。 swapkthnode 在类“GFG”中。对于问题设计者提供的进一步代码(驱动程序代码-除此功能之外),您可以访问我给出的链接,但我认为驱动程序代码与此无关。
  • 无法用您的可用代码重现此问题。该问题可能与链表的错误构建有关。建议你抓NPE,打印链表的内容,找出原因。
  • 我猜你是对的,结束问题! @AlexRudenko

标签: java pointers nullpointerexception segmentation-fault singly-linked-list


【解决方案1】:

对于以下测试,您的代码无法重现该问题:

public static void main(String[] args) {
        int[][] tests = {
                {1,2, 3, 4},
                {1,2, 3, 4, 5},
                {1,2, 3, 4},
                {1,2, 3, 4, 5, 6, 7, 8, 9},
                {1,2, 3, 4, 5, 6, 7, 8, 9},
                {1,2, 3, 4, 5, 6, 7, 8},
                {1,2, 3, 4, 5, 6, 7, 8},
        };

        int[] testK = {1, 3, 4, 3, 2, 3, 2};

        for (int i = 0; i < tests.length; i++) {
            Node head = buildNode(tests[i]);
            System.out.println("\nBefore swap at position " + testK[i] + ": " + dumpNode(head));

            Node result = swapkthnode(head, tests[i].length, testK[i]);

            System.out.println("After swap: " + dumpNode(result));
        }
    }

输出:


Before swap at position 1: 1=>2=>3=>4
After swap: 4=>2=>3=>1

Before swap at position 3: 1=>2=>3=>4=>5
After swap: 1=>2=>3=>4=>5

Before swap at position 4: 1=>2=>3=>4
After swap: 4=>2=>3=>1

Before swap at position 3: 1=>2=>3=>4=>5=>6=>7=>8=>9
After swap: 1=>2=>7=>4=>5=>6=>3=>8=>9

Before swap at position 2: 1=>2=>3=>4=>5=>6=>7=>8=>9
After swap: 1=>8=>3=>4=>5=>6=>7=>2=>9

Before swap at position 3: 1=>2=>3=>4=>5=>6=>7=>8
After swap: 1=>2=>6=>4=>5=>3=>7=>8

Before swap at position 2: 1=>2=>3=>4=>5=>6=>7=>8
After swap: 1=>7=>3=>4=>5=>6=>2=>8

【讨论】:

    【解决方案2】:

    end.next 有可能是null。这可能会导致 NPE 在 while(end.next.next != null) end = end.next;

    如果这符合您的逻辑,请尝试通过执行 end.next != null &amp;&amp; end.next.next != null 来解决此问题。

    【讨论】:

    • 我猜你没有完全理解我的问题或代码的逻辑。我写了第一个条件来检查两件事:1)如果列表只有一个节点 2)如果要交换的节点是中间节点。因此,如果要执行第二个“if”条件:if(K == 1 || K == num),那么列表肯定至少有两个节点,因此end.next 永远不会是null。这个异常是由于我猜其余代码中的某个地方造成的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-01
    • 2012-03-25
    • 1970-01-01
    • 2020-06-01
    • 2022-12-05
    相关资源
    最近更新 更多