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
Print Immutable Linked List in ReverseInput: Immutable Linked List1234head↓ Apply Algorithm ↓Output: Print in Reverse Order4321Can only use printValue() and getNext() methods
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
Asked in
Google 35 Facebook 28 Amazon 22 Microsoft 18
28.5K Views
Medium Frequency
~15 min Avg. Time
843 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen