All prompts are owned by LeetCode. To view the prompt, click the title link above.
First completed : June 06, 2024
Last updated : July 01, 2024
Related Topics : Linked List, Two Pointers, Stack, Recursion
Acceptance Rate : 94.04 %
# 1 - Linear space and time complexity (due to recursive stack being linear)
# Very easy question on just datastructure understanding /shrug
# """
# This is the ImmutableListNode's API interface.
# You should not implement it, or speculate about its implementation.
# """
# class ImmutableListNode:
# def printValue(self) -> None: # print the value of this node.
# def getNext(self) -> 'ImmutableListNode': # return the next node.
class Solution:
def printLinkedListInReverse(self, head: 'ImmutableListNode') -> None:
def helper(currentNode: 'ImmutableListNode') -> None:
if (not currentNode) :
return
helper(currentNode.getNext())
currentNode.printValue()
helper(head)
/**
* Definition for ImmutableListNode.
* struct ImmutableListNode {
* struct ImmutableListNode* (*getNext)(struct ImmutableListNode*); // return the next node.
* void (*printValue)(struct ImmutableListNode*); // print the value of the node.
* };
*/
void printLinkedListInReverse(struct ImmutableListNode* head) {
if (!head) {
return;
}
printLinkedListInReverse(head->getNext(head));
head->printValue(head);
}