punitpawar has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use warnings; use Data::Dumper; ## Singly Linked List my $head = { data => undef, next => undef }; my $curr=$head; for (my $i=1;$i<5;$i++){ $curr->{next} ={ data =>$i, next =>undef }; $curr= $curr->{next}; } $curr->{next} ={ data=>undef, next=>undef }; $curr=$head; while(defined($curr->{next})){ print "$curr->{data} \n"; $curr= $curr->{next}; } $curr=$head; my $forward; my $previous; ## Reversing Singly Linked List while(defined ($curr->{next})){ $forward = $curr->{next}; $curr->{next}=$head; $previous=$head; $curr=$forward; $head=$curr; } $curr->{next}=$previous; $curr=$head; ## printing the reversed List while(defined($curr->{next})){ print "enterd \n"; print "$curr->{data} \n"; $curr= $curr->{next}; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Reversing a singly linked list
by GrandFather (Saint) on Feb 15, 2016 at 00:28 UTC | |
by NetWallah (Canon) on Feb 15, 2016 at 05:35 UTC | |
|
Re: Reversing a singly linked list
by choroba (Cardinal) on Feb 15, 2016 at 10:06 UTC | |
by Anonymous Monk on Feb 15, 2016 at 16:20 UTC | |
by LanX (Saint) on Feb 15, 2016 at 16:46 UTC | |
by punitpawar (Sexton) on Feb 20, 2016 at 13:14 UTC | |
by choroba (Cardinal) on Feb 20, 2016 at 17:51 UTC | |
by Anonymous Monk on Feb 20, 2016 at 20:06 UTC |