Singly-linked list/Element removal: Difference between revisions

Line 388:
After 2nd removal : 1
</pre>
 
=={{header|Perl 6}}==
 
Extending <tt>class Cell</tt> from [[Singly-linked_list/Element_definition#Perl_6]]:
 
<lang perl6> method delete ($value --> Cell) {
my $prev = Nil;
my $cell = self;
my $new-head = self;
while $cell {
my $next = $cell.next;
if $cell.value == $value {
$prev.next = $next if $prev;
$cell.next = Nil;
$new-head = $next if $cell === $new-head;
}
else {
$prev = $cell;
}
$cell = $next;
}
return $new-head;
}</lang>
 
Usage:
 
<lang perl6>my $list = cons 10, (cons 20, (cons 10, (cons 30, Nil)));
 
$list = $list.delete(10);</lang>
 
=={{header|Phix}}==
Anonymous user