I'd be tempted to write this iteratively, but that does require two lists to track input and output, and doesn't vastly improve the code from this recursive version.