You are given an immutable linked list, print its values in reverse.
You are given an interface:
ImmutableListNode {
public void printValue(); // print the value of this node.
public ImmutableListNode getNext(); // return the next node.
}
The linked list is represented by the head ImmutableListNode.
Example 1:
Input: head = [1,2,3,4] Output: [4,3,2,1]
Example 2:
Input: head = [0,-4,-1,3,-5] Output: [-5,3,-1,-4,0]
Example 3:
Input: head = [-2,0,6,4,4,-6] Output: [-6,4,4,6,0,-2]
Constraints:
[1, 1000].[-1000, 1000].Follow up:
Could you solve it with:
When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The challenge is to display a linked list backwards, but we can't change the list itself. The most basic way is to visit each element, store it, and then print the stored elements in reverse order of access.
Here's how the algorithm would work step-by-step:
class Node:
def __init__(self, data):
self.data = data
self.next = None
def print_immutable_linked_list_reverse_brute_force(head):
node_values = []
current_node = head
# Traverse the linked list and store node values
while current_node:
node_values.append(current_node.data)
current_node = current_node.next
# Iterate backwards through the stored values
# to print in reverse order.
for i in range(len(node_values) - 1, -1, -1):
print(node_values[i])Since we can't change the list itself, and we can only move forward, we need a way to visit the end before we start printing. The best way to do this is to dive deep into the list recursively, and then print as we come back out.
Here's how the algorithm would work step-by-step:
class ImmutableListNode:
def __init__(self, val=0, next=None):
self.val = val
self.next = next
def printValue(self):
print(self.val)
def getNext(self):
return self.next
def printLinkedListInReverse(head):
if head:
next_node = head.getNext()
# Recursively call function to reach end of the list
printLinkedListInReverse(next_node)
# Prints the current node's value after the recursive calls return
head.printValue()| Case | How to Handle |
|---|---|
| Null Immutable Linked List | Return without any operation if the head is null. |
| Empty Immutable Linked List | Return without any operation if the list is empty (head is not null, but getNext() returns null on the first node). |
| Single Node Immutable Linked List | Print the value of the single node and return. |
| Immutable Linked List with duplicate values | The algorithm should correctly print all values, regardless of duplicates. |
| Extremely long Immutable Linked List (potential stack overflow with recursion) | Use an iterative approach (e.g., storing values in an array/list then printing in reverse) to avoid stack overflow issues. |
| Immutable Linked List containing maximum integer values | The printing operation should handle maximum integer values correctly without overflow. |
| Immutable Linked List containing negative values | The algorithm should correctly print negative values. |
| Memory constraints for large Immutable Linked Lists | Consider using a divide-and-conquer strategy by processing the list in chunks to avoid excessive memory usage. |