Last active
August 29, 2015 14:02
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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