このアルゴリズムの複雑さは? 私はそれがO(N)だと仮定していますが、明確にしたいと思います。リンクされたリストに末尾がある場合、リストの最後までループする while(current != null) ループを完全に回避するため、これはさらに高速になると思います。
public void reverse() {
Stack<Node> stack = new Stack<Node>();
if (this.head != null || this.head.next != null) {
Node current = this.head;
while (current != null) {
stack.push(current);
current = current.next;
}
Node last = null;
while (!stack.empty()) {
if (last==null) {
this.head = stack.pop();
last = this.head;
continue;
}
last.next = stack.pop();
last = last.next;
if (stack.empty()) {
last.next = null;
}
}
}
}