#include <memory>
struct ListNode;
using List = std::unique_ptr<const ListNode>;
struct ListNode {
const int data;
const List next;
};
List Cons(int head, List tail)
{
return List(new ListNode{head, std::move(tail)});
}
List Nil()
{
return List();
}
size_t len(const List & self)
{
if (!self) {
return 0;
}
return 1 + len(self->next);
}
#include <iostream>
void test(size_t n)
{
auto p = Nil();
for (size_t i = 0; i < n; ++i) {
auto x = std::move(p);
p = Cons(1, std::move(x));
}
std::cout << "done: " << len(p) << std::endl;
}
int main()
{
test(131028);
}
I2luY2x1ZGUgPG1lbW9yeT4KCnN0cnVjdCBMaXN0Tm9kZTsKdXNpbmcgTGlzdCA9IHN0ZDo6dW5pcXVlX3B0cjxjb25zdCBMaXN0Tm9kZT47CgpzdHJ1Y3QgTGlzdE5vZGUgewogICAgY29uc3QgaW50IGRhdGE7CiAgICBjb25zdCBMaXN0IG5leHQ7Cn07CgpMaXN0IENvbnMoaW50IGhlYWQsIExpc3QgdGFpbCkKewogICAgcmV0dXJuIExpc3QobmV3IExpc3ROb2Rle2hlYWQsIHN0ZDo6bW92ZSh0YWlsKX0pOwp9CgpMaXN0IE5pbCgpCnsKICAgIHJldHVybiBMaXN0KCk7Cn0KCnNpemVfdCBsZW4oY29uc3QgTGlzdCAmIHNlbGYpCnsKICAgIGlmICghc2VsZikgewogICAgICAgIHJldHVybiAwOwogICAgfQogICAgcmV0dXJuIDEgKyBsZW4oc2VsZi0+bmV4dCk7Cn0KCiNpbmNsdWRlIDxpb3N0cmVhbT4KCnZvaWQgdGVzdChzaXplX3QgbikKewogICAgYXV0byBwID0gTmlsKCk7CiAgICBmb3IgKHNpemVfdCBpID0gMDsgaSA8IG47ICsraSkgewogICAgICAgIGF1dG8geCA9IHN0ZDo6bW92ZShwKTsKICAgICAgICBwID0gQ29ucygxLCBzdGQ6Om1vdmUoeCkpOwogICAgfQogICAgc3RkOjpjb3V0IDw8ICJkb25lOiAiIDw8IGxlbihwKSA8PCBzdGQ6OmVuZGw7Cn0KCmludCBtYWluKCkKewogICAgdGVzdCgxMzEwMjgpOwp9