我需要该函数返回具有以下数字的列表,因此follow(null, 5);
应该返回[1, 2, 3, 4, 5]
我尝试过:
public static Node <Integer> follow(Node<Integer>list, int num)
{
if(num==1)
return new Node<Integer>(1);
return new Node<Integer>(num, follow(list, num-1));
}
但是这将返回[5, 4, 3, 2, 1]
而不是[1, 2, 3, 4, 5]
,我需要在函数中进行哪些更改,使其返回[1, 2, 3, 4, 5]
? (我不想添加其他功能)
这是节点类:
package unit4.collectionsLib;
public class Node<T>
{
private T info;
private Node<T> next;
public Node(T x)
{
this.info = x;
this.next = null;
}
public Node(T x, Node<T> next)
{
this.info = x;
this.next = next;
}
public T getValue()
{
return(this.info);
}
}
答案 0 :(得分:1)
随着您进一步递归,您正在从5倒数到1。如果您深入递归时从1到5计数怎么办?数字的顺序是什么?
要实现这一点,每个递归步骤都需要有最大数量(在您的示例中为5),因此您需要一个额外的参数。
public static Node <Integer> follow(Node<Integer>list, int num, int max)
{
if(num==max)
return new Node<Integer>(max);
return new Node<Integer>(num, follow(list, num + 1, max));
}
如果您仍然希望在不传递额外参数的情况下获得调用的便利,则可以通过添加该方法的便捷版本来重载该方法:
public static Node <Integer> follow(Node<Integer>list, int max)
{
return follow(list, 1, max);
}
答案 1 :(得分:0)
public static Node <Integer> follow(Node<Integer>list, int num)
{
if(num==1)
return new Node<Integer>(1);
在基本情况下,您正在创建具有最小数量的节点。您如何更改此项以将最小的数字放在列表的前面?
return new Node<Integer>(num, follow(list, num-1));
在递归情况下,您将当前数字添加到列表的前面。您如何更改此项以在末尾添加当前号码?
}
答案 2 :(得分:0)
更新
鉴于您评论的限制,您可以将节点设置为1并开始计数。
package stackoverflow;
public class Question {
public static void main (String [] args){
Node<Integer> list = new Node<Integer>(1);
list.setNext(follow(list, 5));
print(list);
}
public static Node <Integer> follow(Node<Integer>list, int num)
{
if(list.getValue() == num)
return null;
Node<Integer> nextNode = new Node<Integer>(list.getValue()+1);
nextNode.setNext(follow(nextNode, num));
return nextNode;
}
public static void print(Node<Integer> list) {
System.out.println(list.getValue());
if (list.next == null) {
return;
}
print(list.next);
}
public static class Node<T>
{
private T info;
private Node<T> next;
public Node(T x)
{
this.info = x;
this.next = null;
}
public Node(T x, Node<T> next)
{
this.info = x;
this.next = next;
}
public T getValue()
{
return(this.info);
}
public void setNext(Node<T> next) {
this.next = next;
}
}
}
结果
1
2
3
4
5
旧答案
这不是最干净的解决方案,但是考虑到您的要求,您可以在follow方法中计算info字段。我使用了一个类变量max
来知道列表需要多大
package stackoverflow;
public class Question {
static int max = 5;
public static void main (String [] args){
Node<Integer> list = follow(null, max);
print(list);
}
public static Node <Integer> follow(Node<Integer>list, int num)
{
int calculatedInfo = max - (num - 1);
if(calculatedInfo == max)
return new Node<Integer>(max);
return new Node<Integer>(calculatedInfo, follow(list, num-1));
}
public static void print(Node<Integer> list) {
System.out.println(list.info);
if (list.next == null) {
return;
}
print(list.next);
}
public static class Node<T>
{
private T info;
private Node<T> next;
public Node(T x)
{
this.info = x;
this.next = null;
}
public Node(T x, Node<T> next)
{
this.info = x;
this.next = next;
}
public T getValue()
{
return(this.info);
}
}
}
结果
1
2
3
4
5
结果最大值= 10
1
2
3
4
5
6
7
8
9
10
答案 3 :(得分:0)
public static Node <Integer> follow(Node<Integer>list, int num)
{
if(num==1)
return new Node<Integer>(num, list);
return follow(new Node<Integer>(num, list), num-1);
}
这将返回[1, 2, 3, 4, 5]