(The last node points to undef .. there is no empty node)use strict; use warnings; use Data::Dumper; my $forwardList = buildList(1 .. 4); my $reverseList = buildList(reverse getList($forwardList)); print join (', ', getList($forwardList)), "\n";; print join (', ', getList($reverseList)), "\n";; sub buildList { my ($head, $node); for (@_){ $node = add_node($node,$_); $head ||= $node; } return $head; } sub add_node{ my ($prev, $data) = @_; return $prev->{next} ={data=>$data, next=>undef}; } sub getList { my ($head) = @_; my @data = ($head->{data}); while ( $head = $head->{next}) { push @data, $head->{data}; } return @data; }
"Think of how stupid the average person is, and realize half of them are stupider than that." - George Carlin
In reply to Re^2: Reversing a singly linked list
by NetWallah
in thread Reversing a singly linked list
by punitpawar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |