LeetCode 1265: Print Immutable Linked List in Reverse Solution

Master LeetCode problem 1265 (Print Immutable Linked List in Reverse), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1265. Print Immutable Linked List in Reverse

Problem Explanation

Explanation:

To print an immutable singly linked list in reverse order, we can use a recursive approach. The idea is to first find the length of the linked list by iterating through it, and then recursively print the nodes in reverse order by traversing the list from the end to the beginning.

Here is the step-by-step algorithm:

  1. Find the length of the linked list by iterating through it.
  2. Define a recursive function that takes the current node and the remaining length as parameters.
  3. In the recursive function, if the remaining length is greater than 1, recursively call the function with the next node and decremented remaining length.
  4. Print the value of the current node.

Time Complexity:

  • Finding the length of the linked list takes O(n) time.
  • Printing the nodes in reverse order using recursion also takes O(n) time.
  • Therefore, the overall time complexity is O(n).

Space Complexity:

  • The space complexity is O(n) due to the recursive calls on the call stack.

:

Solution Code

// Java Solution
/**
 * // This is the ImmutableListNode's API interface.
 * // You should not implement it, or speculate about its implementation.
 * interface ImmutableListNode {
 *     public void printValue(); // print the value of this node.
 *     public ImmutableListNode getNext(); // return the next node.
 * }
 */

class Solution {
    public void printLinkedListInReverse(ImmutableListNode head) {
        int length = 0;
        ImmutableListNode current = head;
        while (current != null) {
            length++;
            current = current.getNext();
        }
        printRecursiveReverse(head, length);
    }
    
    private void printRecursiveReverse(ImmutableListNode node, int remaining) {
        if (node == null || remaining < 1) {
            return;
        }
        printRecursiveReverse(node.getNext(), remaining - 1);
        node.printValue();
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1265 (Print Immutable Linked List in Reverse)?

This page provides optimized solutions for LeetCode problem 1265 (Print Immutable Linked List in Reverse) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1265 (Print Immutable Linked List in Reverse)?

The time complexity for LeetCode 1265 (Print Immutable Linked List in Reverse) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1265 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1265 in Java, C++, or Python.

Back to LeetCode Solutions