I need the function to return a list with following numbers so follow(null, 5);
should return [1, 2, 3, 4, 5]
I tried this:
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));
}
but this returns [5, 4, 3, 2, 1]
instead of [1, 2, 3, 4, 5]
what do I need to change in the function so it will return [1, 2, 3, 4, 5]
? (I dont want to add another functions)
This is the Node Class:
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);
}
}