I ran into an error while making a toString method for a linked list of 'a' to 'z'. The result String returned back to the public method only contains 'a' when the result println shows a-z public String toString() { String result = "{"; result = generateList(result, first); result += "}"; return result; } private String generateList(String result, Node first) { result += "," + first.data; System.out.println(result); //tests what result contains at each iteration if(first.next != null) { generateList(result, first.next); } ret
last part is "return result;" I think the fix to this is to put the return statement of the private method in an else statement, but this results in an error.
It appears result = generateList(); works..
Join our real-time social learning platform and learn together with your friends!