I need to code a method that prints the contents of a linked list recursively, however my method isn't working.
public LinkedList reverse() {
LinkedList list = new LinkedList();
list = reverseRecursion(list, 0);
return list;
}
public LinkedList reverseRecursion(LinkedList list, int pos) {
if (pos != size()) {
rotate();
list.add(get());
pos = pos + 1;
reverseRecursion(list, pos);
}
return list;
}
public String toString() {
String result = "Contents of list: {";
result = toStringRecursion(result, 0);
return result;
}
private String toStringRecursion(String result, int pos) {
if (pos != size()) {
rotate();
String temp = result + get().getData() + ",";
pos = pos + 1;
toStringRecursion(temp, pos);
}
return result;
}
the reverse function is working fine, however my toString is only showing up as the initial result "Contents of list: {", despite being passed the previous result as a parameter. If I print the result each time I pass through the toStringRecursion, I can see the desired output right before it reverts back to the inital result when the return line is reached.