Skip to main content
LeetCode 1265, Medium. Topics: Stack, Recursion, Linked List, Two Pointers. View on LeetCode. Generate this problem as a practice environment: tested reference solution, 14 parametrized pytest cases, and a playground notebook:

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. The Python harness models the judge: each print_value() call records the node value, and the recorded values are compared with the expected reversed sequence.

Examples

Constraints

  • The length of the linked list is between [1, 1000].
  • The value of each node in the linked list is between [-1000, 1000].
Follow up:
  • Could you solve this problem in constant space complexity?
  • Could you solve this problem in linear time complexity and less than linear space complexity?

Solution

Reference implementation from solution.py on GitHub, full suite in test_solution.py:

Complexity

Tags

NeetCode All.
Last modified on September 7, 2026