工程技术研究中心网站建设要求,WordPress书主题,绍兴建设开发有限公司网站首页,商城网站建设付款怎么实现文章目录1. 题目2. 解题1. 题目
给您一个不可变的链表#xff0c;使用下列接口逆序打印每个节点的值#xff1a;
ImmutableListNode: 描述不可变链表的接口#xff0c;链表的头节点已给出。
您需要使用以下函数来访问此链表#xff08;您 不能 直接访问 ImmutableListNo…
文章目录1. 题目2. 解题1. 题目
给您一个不可变的链表使用下列接口逆序打印每个节点的值
ImmutableListNode: 描述不可变链表的接口链表的头节点已给出。
您需要使用以下函数来访问此链表您 不能 直接访问 ImmutableListNode
ImmutableListNode.printValue()打印当前节点的值。ImmutableListNode.getNext()返回下一个节点。
输入只用来内部初始化链表。您不可以通过修改链表解决问题。 也就是说您只能通过上述 API 来操作链表。
进阶 您是否可以 使用常数级空间复杂度解决问题 使用线性级时间复杂度和低于线性级空间复杂度解决问题
示例 1
输入head [1,2,3,4]
输出[4,3,2,1]示例 2
输入head [0,-4,-1,3,-5]
输出[-5,3,-1,-4,0]示例 3
输入head [-2,0,6,4,4,-6]
输出[-6,4,4,6,0,-2]提示
链表的长度在 [1, 1000] 之间。
每个节点的值在 [-1000, 1000] 之间。来源力扣LeetCode 链接https://leetcode-cn.com/problems/print-immutable-linked-list-in-reverse 著作权归领扣网络所有。商业转载请联系官方授权非商业转载请注明出处。 2. 解题
/*** // This is the ImmutableListNodes API interface.* // You should not implement it, or speculate about its implementation.* class ImmutableListNode {* public:* void printValue(); // print the value of the node.* ImmutableListNode* getNext(); // return the next node.* };*/class Solution {//C
public:void printLinkedListInReverse(ImmutableListNode* head) {if(!head)return;printLinkedListInReverse(head-getNext());head-printValue();}
};0 ms 6.8 MB
#
# This is the ImmutableListNodes 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: # py3def printLinkedListInReverse(self, head: ImmutableListNode) - None:if not head:returnself.printLinkedListInReverse(head.getNext())head.printValue()56 ms 14.3 MB 长按或扫码关注我的公众号Michael阿明一起加油、一起学习进步