Print Immutable Linked List in Reverse - Problem
You are given an immutable linked list, print out all values of each node in reverse with the help of the following interface:
ImmutableListNode: An interface of immutable linked list, you are given the head of the list.
You need to use the following functions to access the linked list (you can't access the ImmutableListNode directly):
ImmutableListNode.printValue(): Print value of the current node.ImmutableListNode.getNext(): Return the next node.
The input is only given to initialize the linked list internally. You must solve this problem without modifying the linked list. In other words, you must operate the linked list using only the mentioned APIs.
Input & Output
Example 1 — Basic List
$
Input:
head = [1,2,3,4]
›
Output:
4
3
2
1
💡 Note:
Print each value in reverse order: start with 4 (last node), then 3, 2, and finally 1 (first node)
Example 2 — Single Node
$
Input:
head = [1]
›
Output:
1
💡 Note:
Only one node, so print its value: 1
Example 3 — Two Nodes
$
Input:
head = [1,2]
›
Output:
2
1
💡 Note:
Print in reverse: 2 (second node) then 1 (first node)
Constraints
- 1 ≤ Number of nodes ≤ 1000
- 0 ≤ Node.val ≤ 1000
Visualization
Tap to expand
Understanding the Visualization
1
Input
Immutable linked list: 1→2→3→4
2
Process
Use recursion or stack to reverse printing order
3
Output
Print values: 4, 3, 2, 1
Key Takeaway
🎯 Key Insight: Recursion's call stack naturally provides the reverse order we need
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code