Create the function linkedListMultiplier: Problem regarding Linklist in PHP. Traversing the data value in the linklist

Create the function linkedListMultiplier: Problem regarding Linklist in PHP. Traversing the data value in the linklist

  • Post author:
  • Post category:PHP

function linkedListMultiplierCheck( $arr, $init ) {
$head = new Node($arr[0],
new Node($arr[1],
new Node($arr[2],
new Node($arr[3], NULL))));

return linkedListMultiplier($head, $init);
}

class Node {
public $data, $next;
public function __construct($data, $next = NULL) {
list($this->data, $this->next) = [$data, $next];

}
}

// DO NOT MODIFY THE CODE ABOVE
function linkedListMultiplier($head, $init){
$loop = true;
$respone = [];
while($loop){
$init = $head->data * $init;
$respone[] = $init;
if($head->next == ''){
$loop = false;
}else{
$head = $head->next;
}
}
return $respone;
}
$head = linkedListMultiplierCheck([2,3,4,5], 1);
linkedListMultiplier($head,1);