当前位置:网站首页>剑指 Offer 06. 从尾到头打印链表
剑指 Offer 06. 从尾到头打印链表
2022-07-05 08:16:00 【程序员·小李】
题目:
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
示例 1:
输入:head = [1,3,2]
输出:[2,3,1]
限制:
0 <= 链表长度 <= 10000
思路:
使用栈来完成链表的遍历,最后依次弹栈获取结果。
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
Stack<ListNode> stack = new Stack<>();
int counter = 0;
while(head != null){
counter++;
stack.push(head);
head = head.next;
}
int[] array = new int[counter];
for (int i = 0; i < counter; i++){
array[i] = stack.pop().val;
}
return array;
}
}
边栏推荐
- 亿学学堂给的证券账户安不安全?哪里可以开户
- Zero length array in GNU C
- 【云原生 | 从零开始学Kubernetes】三、Kubernetes集群管理工具kubectl
- Carrier period, electrical speed, carrier period variation
- Volatile of C language
- Slist of linked list
- Some thoughts on extracting perspectives from ealfa and Ebeta
- Live555 push RTSP audio and video stream summary (I) cross compilation
- Semiconductor devices (I) PN junction
- Halcon's practice based on shape template matching [2]
猜你喜欢
Several important parameters of LDO circuit design and type selection
Circleq of linked list
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
STM32 single chip microcomputer -- volatile keyword
STM32 single chip microcomputer - bit band operation
Design a clock frequency division circuit that can be switched arbitrarily
实例004:这天第几天 输入某年某月某日,判断这一天是这一年的第几天?
STM32 single chip microcomputer -- debug in keil5 cannot enter the main function
Drive LED -- GPIO control
随机推荐
matlab timeserise
Class of color image processing based on Halcon learning_ ndim_ norm. hdev
C WinForm [realize the previous and next selection pictures] - practice 7
Matlab2018b problem solving when installing embedded coder support package for stmicroelectronic
Why is 1900 not a leap year
Briefly talk about the identification protocol of mobile port -bc1.2
STM32 virtualization environment of QEMU
On boost circuit
VESC Benjamin test motor parameters
Detailed explanation of SQL server stored procedures
Connection mode - bridge and net
Factors affecting the quality of slip rings in production
Imx6ull bare metal development learning 2- use C language to light LED indicator
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
Ble encryption details
WiFi wpa_ Detailed description of supplicant hostpad interface
OC and OD gate circuit
C language # and #
Several important parameters of LDO circuit design and type selection
Hardware 3 -- function of voltage follower