LeetCode 1265. Print Immutable Linked List in Reverse

原题链接在这里:https://leetcode.com/problems/print-immutable-linked-list-in-reverse/description/
题目:

You are given an immutable linked list, print out all values of each node in reverse with the help of the following interface:

  • ImmutableListNode: An interface of immutable linked list, you are given the head of the list.

You need to use the following functions to access the linked list (you can't access the ImmutableListNode directly):

  • ImmutableListNode.printValue(): Print value of the current node.
  • ImmutableListNode.getNext(): Return the next node.

The input is only given to initialize the linked list internally. You must solve this problem without modifying the linked list. In other words, you must operate the linked list using only the mentioned APIs.

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:

  • The length of the linked list is between [1, 1000].
  • The value of each node in the linked list is between [-1000, 1000].

Follow up:

Could you solve this problem in:

  • Constant space complexity?
  • Linear time complexity and less than linear space complexity?

题解:

We can get to the last node and then print it. We can do this recursively.

Time Complexity: O(n). n is the length of list.

Space: O(n). stack space.

AC Java:

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

We can also do it iteratively.

Time Complexity: O(n).

Space: O(n).

AC Java:

 1 /**
 2  * // This is the ImmutableListNode's API interface.
 3  * // You should not implement it, or speculate about its implementation.
 4  * interface ImmutableListNode {
 5  *     public void printValue(); // print the value of this node.
 6  *     public ImmutableListNode getNext(); // return the next node.
 7  * };
 8  */
 9 
10 class Solution {
11     public void printLinkedListInReverse(ImmutableListNode head) {
12         Stack<ImmutableListNode> stk = new Stack<>();
13         while(head != null){
14             stk.push(head);
15             head = head.getNext();
16         }
17 
18         while(!stk.isEmpty()){
19             stk.pop().printValue();
20         }
21     }
22 }

Follow up to use constant space complexity. Then every time, we can get to the nth node and print it. Begin with the last node.

Time Complexity: O(n^2).

Space: O(1).

AC Java:

 1 /**
 2  * // This is the ImmutableListNode's API interface.
 3  * // You should not implement it, or speculate about its implementation.
 4  * interface ImmutableListNode {
 5  *     public void printValue(); // print the value of this node.
 6  *     public ImmutableListNode getNext(); // return the next node.
 7  * };
 8  */
 9 
10 class Solution {
11     public void printLinkedListInReverse(ImmutableListNode head) {
12         int length = getLength(head);
13         for(int i = length; i >= 1; i--){
14             printNthNode(head, i);
15         }
16     }
17 
18     private int getLength(ImmutableListNode head){
19         int res = 0;
20         while(head != null){
21             res++;
22             head = head.getNext();
23         }
24 
25         return res;
26     }
27 
28     private void printNthNode(ImmutableListNode head, int pos){
29         for(int i = 1; i < pos; i++){
30             head = head.getNext();
31         }
32 
33         head.printValue();
34     }
35 }

For linear time complexity and less than linear space compleixty. We can think from stack. How about we equally divide the list and only store the first node of that each set.

Here we can choose the set size as sqrt(n). Then totally we need sqrt(n) stack space. 

When we pop a node, we can have another stack printing from the last. The second stack also takes sqrt(n) space.

posted @ 2024-03-18 05:35  Dylan_Java_NYC  阅读(3)  评论(0编辑  收藏  举报