Random tests in PHP create DEAD LOOPS. Because same nodes uesed in a different tests and point on each other. For example 70->null
and 8->19->null
. Get answer 70 -> 8 -> 19 -> null
. Then take 8 -> 19 the middle of ALREADY EXISTED LIST and ask to prepend it to the head 70
. Or append head to a tail. Eventually DEAD LOOP appears: 70->8->19->70...
.
Start---------------
A:
70->null
B:
8->19->null
Now all A must be marked
70.->null
Ans:
70.->8->19->null
Start---------------
A:
8->19->null
B:
70.->8->19->null
Now all A must be marked
8.->19.->null
Ans:
8.->19.->70.->8.->19.->70.->8.->19.->70.->8.->19.->70.->
<?php
# https://www.codewars.com/kata/55d17ddd6d7868493e000074 Linked Lists - Append.
function append($list_a, $list_b) {
echo "Start---------------\n";
echo "A: \n";
prnt($list_a);
echo "B: \n";
prnt($list_b);
if(is_null($list_a) && is_null($list_b)) return NULL;
if(is_null($list_a)) return $list_b;
$x = $list_a;
while( !is_null($x->next)){
$x->mark = true;
$x = $x->next;
}
$x->mark = true;
echo "Now all A must be marked\n";
prnt($list_a);
$x->next = $list_b;
echo "Ans: \n";
prnt($list_a);
return $list_a;
}
function prnt($l){
while($l){ echo $l->data;
if(isset($l->mark)) echo ".";
echo "->";
$l = $l->next; }
echo "null \n";
}