- https://www.codewars.com/kata/55cacc3039607536c6000081 Linked Lists - Insert Nth Node.
Started at 22:45 22.08.2020 Aug Saturday.
Finished at 22.08.2020 23:13 Aug Saturday. (0hrs 28minutes)
<?php | |
# https://www.codewars.com/kata/55cacc3039607536c6000081 Linked Lists - Insert Nth Node. | |
function insert_nth($head, $index, $data) { | |
if(!$head) return new Node($data); | |
if(0 == $index) return new Node($data,$head); | |
$prv = null; $nxt = $head; $input = new Node($data); | |
for($i = 0; $i < $index && $nxt; $i += 1){ | |
$prv = $nxt; $nxt = $nxt->next; | |
} | |
if($i != $index) throw new InvalidArgumentException('Division by zero.'); | |
$prv->next = $input; | |
$input->next = $nxt; | |
return $head; | |
} |
Started at 22:45 22.08.2020 Aug Saturday.
Finished at 22.08.2020 23:13 Aug Saturday. (0hrs 28minutes)