3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class Node { public $val; public $next; public function __construct($val, $next) { $this->val = $val; $this->next = $next; } public function __toString() { $a = []; $c = $this; while ($c != null) { $a[] = $c->val; $c = $c->next; } return implode(", ", $a)."\n"; } public static function reverse($list) { $newl = null; $curr = $list; while ($curr != null) { $next = $curr->next; $curr->next = $newl; $newl = $curr; $curr = $next; } return $newl; } public static function reverseRecursively($list) { if ($list == null || $list->next == null) { return $list; } $next = $list->next; $list->next = null; $newl = self::reverseRecursively($next); $newl->next = $list; return $newl; } } $list = new Node(1, new Node(2, new Node(3, new Node(4, new Node(5, null))))); echo $list; $list = Node::reverse($list); echo $list; exit();
based on 7LHYV
Output for 5.6.0 - 5.6.30, hhvm-3.15.4, 7.0.0 - 7.2.6
1, 2, 3, 4, 5 5, 4, 3, 2, 1