Skip to content

Instantly share code, notes, and snippets.

@koras
Last active August 29, 2015 14:18
Show Gist options
  • Save koras/03e3f9d99fd06bb544ec to your computer and use it in GitHub Desktop.
Save koras/03e3f9d99fd06bb544ec to your computer and use it in GitHub Desktop.

Обращение списка

Написать (на любом языке функцию), которая обращает список.

NB: Решения через array_reverse, array_map и прочие array_штуковины не принимаются.

Пример на php:

<?php

final class ListElement {

    private $id;
    private $next;

    public function __construct($id) {
        $this->id = $id;
    }

    public function getId() {
        return $this->id;
    }

    public function getNext() {
        return $this->next;
    }

    public function setNext(ListElement $next) {
        $this->next = $next;
        return $this->next;
    }
}

$list = new ListElement(1);
$last = $list
    ->setNext(new ListElement(2))
    ->setNext(new ListElement(4))
    ->setNext(new ListElement(7)); // 8, 9 ... 9001 и т.д.

// $last->setNext($list); // так тоже бывает
 
function f($list) {
    // Тут решение
}

$reverse = f($list);
print_r($reverse);
var_dump(spl_object_hash($reverse) == spl_object_hash(f(f($reverse)))); // true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment