AcWing 17. 从尾到头打印链表
原题链接
简单
作者:
小轩喵灬
,
2025-01-09 23:06:18
,
所有人可见
,
阅读 1
/**
* Definition for singly-linked list.
* class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] printListReversingly(ListNode head) {
Stack<Integer> stack = new Stack<>();
while(head != null) {
stack.add(head.val);
head = head.next;
}
List<Integer> list = new ArrayList<>();
while(!stack.isEmpty()) {
list.add(stack.pop());
}
return list.stream().mapToInt(Integer::intValue).toArray();
}
}