I have a simple Immutable Linked list class the following:
class Node<T> {
final T val;
final Node next;
Node(T val, Node next) {
this.val = val;
this.next = next;
}
}
I would like to create an instance which has itself as the next node, thereby creating a circular list.
I obviously can't do Node x = new Node<>(0, x)
, since x
won't be defined by the time it should be passed to the constructor.
Are there any tricks that'd allow me to create this without changing Node
?
For example, http://stackoverflow.com/a/25473855/205521 describes how to do something similar for lambda functions, that is a function which refers to itself (recursion). I, however, wasn't able to make it work for initialization.