题目链接:https://leetcode-cn.com/problems/cong-wei-dao-tou-da-yin-lian-biao-lcof/

思路解题
这个题目其实就是一个先进后出
,借助辅助栈即可。
代码如下
public class ListNode {
int val;
ListNode next;
ListNode(int x) {
val = x;
}
}
public int[] reversePrint(ListNode head) {
LinkedList<Integer> list = new LinkedList<>();
ListNode node = head;
while(node!=null){
list.push(node.val);
node = node.next;
}
int size = list.size();
int []array = new int[size];
for (int i=0;i<size;i++){
array[i] = list.pop();
}
return array;
}
复杂度分析
时间复杂度:O(n)。
空间复杂度:O(n)。
网友评论