为什么我的代码抛出 NullPointerException,尽管它的逻辑很好并且也通过了示例测试用例?
Why my code is throwing NullPointerException though it's fine with logic and also passes the sample testcases?
我们的任务是完成函数swapkthnode()
这段代码有什么问题?它适用于自定义检查问题本身给出的示例 testcases/example。我已经在在线 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
有可能 end.next
是 null
。这可能导致 while(end.next.next != null) end = end.next;
的 NPE
如果符合您的逻辑,请尝试通过 end.next != null && end.next.next != null
来解决此问题。
无法用您的代码重现以下测试的问题:
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
我们的任务是完成函数swapkthnode()
这段代码有什么问题?它适用于自定义检查问题本身给出的示例 testcases/example。我已经在在线 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
有可能 end.next
是 null
。这可能导致 while(end.next.next != null) end = end.next;
如果符合您的逻辑,请尝试通过 end.next != null && end.next.next != null
来解决此问题。
无法用您的代码重现以下测试的问题:
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