in reply to Re^2: Reversing a singly linked list
in thread Reversing a singly linked list

You are right. You can either assign the new list to the old one:
$head = $curr;

The old nodes will be freed, as there's no reference to the head remaining afterwards. Once the head is gone, the next node can be freed, too, and so on.

Or, you can use a bit more complicated procedure to invert the list in place:

$curr = $head; my $tail; while ($curr) { my $next = $curr->{next}; $curr->{next} = $tail; $tail = $curr; $curr = $next; }

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,

Replies are listed 'Best First'.
Re^4: Reversing a singly linked list
by Anonymous Monk on Feb 20, 2016 at 20:06 UTC

    Awww! Can't believe you missed the opportunity for a list assignment here. :)

    ($curr->{next}, $tail, $curr) = ($tail, $curr, $curr->{next});