Skip to content

Latest commit

 

History

History
71 lines (54 loc) · 2.05 KB

_1265. Print Immutable Linked List in Reverse.md

File metadata and controls

71 lines (54 loc) · 2.05 KB

All prompts are owned by LeetCode. To view the prompt, click the title link above.

Back to top


First completed : June 06, 2024

Last updated : July 01, 2024


Related Topics : Linked List, Two Pointers, Stack, Recursion

Acceptance Rate : 94.04 %


Solutions

Python

# 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)

C

/**
 * 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);
}