0
Can anyone tell me why I'm getting an error "Terminated Execution Timed out"?
2 ответов
+ 1
(1)(EDITED) When head is null you assign data to head. You don't need to use new Node but assign data to data in the node and do that for the others. Also, the method should end right there since you've already add data to an element. Add a return statement to end the method.
(2) I suggest you use StringBuilder instead of String for concatenation. It's a lot faster. But remember to use append() instead of += and use output.toString() at the return statement.
(3) Use while(currentnode != null) instead of next. This avoid the last element is not printed.
(4) Use currentnode.data instead of next in string concatenation. Node itself doesn't have toString() therefore can't be used.