Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
public class Node<T> {
private T a;
private Node next;
public Node(T a) {
this.a = a;
}
// 先頭に追加する
Node add(T n) {
return new Node(n).setNext(this);
}
private Node setNext(Node n) {
this.next = n;
return this;
}
int length() {
if (next == null) {
return 1;
} else {
return 1 + this.next.length();
}
}
@Override
public String toString() {
return this.next != null
? this.a.toString() + "," + this.next.toString()
: this.a.toString() + ",Nil";
}
public static void main(String[] args) {
Node<Integer> n = new Node<Integer>(3).add(2).add(1);
System.out.println(n);
System.out.println(n.length());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment