Skip to content

Instantly share code, notes, and snippets.

@chadluo
Last active August 29, 2015 14:02
Show Gist options
  • Save chadluo/2cd590e1456058fb9f2e to your computer and use it in GitHub Desktop.
Save chadluo/2cd590e1456058fb9f2e to your computer and use it in GitHub Desktop.
reverse stack recursively at O(n) time and O(n) space. reimplementation of http://www.zhihu.com/question/24020049/answer/26440695
#!/usr/bin/env perl
use strict;
use warnings;
sub rev {
my $q = pop;
if (scalar @_ == 0) {
return @{$q};
} else {
push @{$q}, pop;
rev(@_,$q);
}
}
print join(",",rev((1,2,3,10,1000),[]))."\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment