How do you reverse a linked list using recursion, and return it as a string?
String backwards(IntNode head): Create and return a string containing the values in the input list, separated by commas, in reverse order. The last number should not be followed by a comma. Spacing within the string is not important. For example, if head points to the list: 12 45 -3 10 99 -47, the expression
OneListRec.backwards(head)
should return the string "-47, 99, 10, -3, 45, 12".
i am confused as to how to do this. I tried to reverse the list recursively:
public static String backwards(IntNode head)
{
if (head == null) {
return null;
}
if (head.next == null) {
return head;
}
reverse(head.next);
head.next.next = head;
head.next = null;
return head;
}
i know this is wrong but i tried, but how do you return it as a string ?