Skip to content

Instantly share code, notes, and snippets.

@telless
Created February 24, 2018 09:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save telless/b5a826b4b83cb0edde6fdf7c87522d34 to your computer and use it in GitHub Desktop.
Save telless/b5a826b4b83cb0edde6fdf7c87522d34 to your computer and use it in GitHub Desktop.
<?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