I am trying to write an function incrList(L, x)
that copies a given Linear Linked List, L, recursively and increment by a constant value, x. When I compile in terminal I got IntList@6ff3c5b5
, which is a memory location, instead of the actual list. I only want to change the function incrList
itself to give the right output.
public class IntList {
public int first;
public IntList rest;
public IntList(int f, IntList r) {
first = f;
rest = r;
}
}
public class Lists1Exercises{
public static IntList incrList(IntList L, int x) {
if (L == null){
return null;
}else {
IntList head = new IntList(L.first+x, null);
head.rest = incrList(L.rest, x);
return head;
}
}
public static void main(String[] args) {
IntList L = new IntList(5, null);
L.rest = new IntList(7, null);
L.rest.rest = new IntList(9, null);
System.out.println(incrList(L, 3));
}
}