跳转至

1265. 逆序打印不可变链表 🔒

题目描述

给您一个不可变的链表,使用下列接口逆序打印每个节点的值:

  • 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] 之间。

 

进阶:

您是否可以:

  • 使用常数级空间复杂度解决问题?
  • 使用线性级时间复杂度和低于线性级空间复杂度解决问题?

解法

方法一:递归

我们可以使用递归来实现链表的逆序打印。在函数中,我们判断当前节点是否为空,如果不为空,则获取下一个节点,然后递归调用函数本身,最后打印当前节点的值。

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是链表的长度。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# """
# 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:
        if head:
            self.printLinkedListInReverse(head.getNext())
            head.printValue()
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
/**
 * // 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) {
        if (head != null) {
            printLinkedListInReverse(head.getNext());
            head.printValue();
        }
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
/**
 * // This is the ImmutableListNode's 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 {
public:
    void printLinkedListInReverse(ImmutableListNode* head) {
        if (head) {
            printLinkedListInReverse(head->getNext());
            head->printValue();
        }
    }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
/*   Below is the interface for ImmutableListNode, which is already defined for you.
 *
 *   type ImmutableListNode struct {
 *
 *   }
 *
 *   func (this *ImmutableListNode) getNext() ImmutableListNode {
 *      // return the next node.
 *   }
 *
 *   func (this *ImmutableListNode) printValue() {
 *      // print the value of this node.
 *   }
 */

func printLinkedListInReverse(head ImmutableListNode) {
    if head != nil {
        printLinkedListInReverse(head.getNext())
        head.printValue()
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
/**
 * // This is the ImmutableListNode's API interface.
 * // You should not implement it, or speculate about its implementation
 * class ImmutableListNode {
 *      printValue() {}
 *
 *      getNext(): ImmutableListNode {}
 * }
 */

function printLinkedListInReverse(head: ImmutableListNode) {
    if (head) {
        printLinkedListInReverse(head.next);
        head.printValue();
    }
}
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
/**
 * // This is the ImmutableListNode's API interface.
 * // You should not implement it, or speculate about its implementation.
 * class ImmutableListNode {
 *     public void PrintValue(); // print the value of this node.
 *     public ImmutableListNode GetNext(); // return the next node.
 * }
 */

public class Solution {
    public void PrintLinkedListInReverse(ImmutableListNode head) {
        if (head != null) {
            PrintLinkedListInReverse(head.GetNext());
            head.PrintValue();
        }
    }
}

评论