【发布时间】:2018-01-04 02:19:30
【问题描述】:
我找不到代码有什么问题,但是当我提交它时,两个测试用例给出了运行时错误。请帮我找出那个错误。我已经检查了至少 30 个自定义测试用例,但它为所有测试用例提供了正确的输出。
Code
public static Node mergeTwoList(Node head1, Node head2) {
Node c = null;
if (head1 == null) {
return head2;
} else if (head2 == null) {
return head1;
}
if (head1.data < head2.data) {
c = head1;
c.next = mergeTwoList(head1.next, head2);
} else {
c = head2;
c.next = mergeTwoList(head1, head2.next);
}
return c;
}
如果有人知道什么,请告诉我。
【问题讨论】:
-
错误是什么?
-
在线工具没有给出具体细节。它只是显示运行时错误
-
@KillerDeath 可能是由于递归导致的堆栈溢出。你找到什么了吗?
-
你的规范是否允许你改变这两个列表?还是需要您提供第三份清单?
-
@Prince 解决方案看起来不错,应该会产生预期的输出。但首先阅读有关在线环境时间和内存限制的信息。这可能是因为堆栈溢出,因为在这两个测试用例中给定的输入太长了。所以我建议我们使用迭代解决方案而不是递归解决方案来让这些测试通过。
标签: java recursion data-structures linked-list