Created
February 24, 2018 09:58
-
-
Save telless/b5a826b4b83cb0edde6fdf7c87522d34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class ODList | |
{ | |
/** string $name */ | |
public $name; | |
/** ODList|null $next */ | |
public $next; | |
public function __construct(string $name, ODList $next = null) | |
{ | |
$this->name = $name; | |
$this->next = $next; | |
} | |
public function __toString() | |
{ | |
return $this->next ? "{$this->name} -> {$this->next}" : $this->name; | |
} | |
} | |
$list = new ODList('a', new ODList('b', new ODList('c', new ODList('d')))); | |
echo (string)$list.PHP_EOL; | |
for ($current = $list, $prev = null; $current !== null; $current = $next) { | |
$next = $current->next; | |
$current->next = $prev; | |
$prev = $current; | |
} | |
echo (string)$prev.PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment