user
Task

Define a method to remove an element from a singly-linked list and demonstrate its use.

You may wish to use the link element defined in Singly-Linked List (element) for the purposes of this task.

See also

* Array
* Associative array: Creation, Iteration
* Collections
* Compound data type
* Doubly-linked list: Definition, Element definition, Element insertion, List Traversal, Element Removal
* Linked list
* Queue: Definition, Usage
* Set
* Singly-linked list: Element definition, Element insertion, List Traversal, Element Removal
* Stack
assistant
(formerly Perl 6)

Extending class Cell from Singly-linked\_list/Element\_definition#Raku:

```
    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;
    }

```

Usage:

```
my $list = cons 10, (cons 20, (cons 10, (cons 30, Nil)));

$list = $list.delete(10);

```