1265. Print Immutable Linked List in Reverse
You are given an immutable singly linked list. You may not modify it, and you cannot access its fields directly. Instead you are given only these two operations on a node:
ImmutableListNode.printValue(): print the value of the current node.ImmutableListNode.getNext(): return the next node, or a null marker at the tail.
Print every value of the list in reverse order, from the tail back to the head, using only those two operations.
Transport adapter
The platform cannot pass a live ImmutableListNode object, so the node values are supplied as the array arg1 in head-to-tail order. Treat that array as the immutable list and preserve the original restrictions:
- The list is read-only, so its order must never be rewritten in place.
- Values may be consumed only by a forward walk that stands in for repeated
getNext()calls. Do not index the array from the end and do not reverse it by swapping elements. - The returned array is the transport form of the
printValue()output stream.
The reference solutions therefore either descend the forward walk with recursion or push each forward value onto an explicit stack, and only then produce the reversed output.
1265. Print Immutable Linked List in Reverse
You are given an **immutable** singly linked list. You may not modify it, and you cannot access its fields directly. Instead you are given only these two operations on a node: