当前位置:网站首页>剑指 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;
}
}
边栏推荐
- PMSM dead time compensation
- Measurement fitting based on Halcon learning [III] PM_ measure_ board. Hdev routine
- DokuWiki deployment notes
- Explication de la procédure stockée pour SQL Server
- VESC Benjamin test motor parameters
- Fundamentals of C language
- go依赖注入--google开源库wire
- Keil use details -- magic wand
- OLED 0.96 inch test
- STM32 virtualization environment of QEMU
猜你喜欢

More than 90% of hardware engineers will encounter problems when MOS tubes are burned out!

Detailed summary of FIO test hard disk performance parameters and examples (with source code)

Embedded composition and route

Introduction of air gap, etc
![C WinForm [help interface - send email] - practice five](/img/2a/c4e7abe054e6fdd45acc7d297a033d.jpg)
C WinForm [help interface - send email] - practice five

实例010:给人看的时间

Talk about the function of magnetic beads in circuits
![Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine](/img/99/21c228ff5de46c4a42b60f989b10e8.jpg)
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine

Step motor generates S-curve upper computer

Class of color image processing based on Halcon learning_ ndim_ norm. hdev
随机推荐
Wifi-802.11 negotiation rate table
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
Shape template matching based on Halcon learning [v] find_ cocoa_ packages_ max_ deformation. Hdev routine
Basic embedded concepts
[trio basic from introduction to mastery tutorial 20] trio calculates the arc center and radius through three points of spatial arc
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address
Bluetooth hc-05 pairing process and precautions
Keil use details -- magic wand
DokuWiki deployment notes
STM32 single chip microcomputer - bit band operation
Weidongshan Internet of things learning lesson 1
PMSM dead time compensation
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
Brief discussion on Buck buck circuit
OLED 0.96 inch test
Sizeof (function name) =?
99 multiplication table (C language)
Stm32--- systick timer
实例009:暂停一秒输出
Shell script realizes the reading of serial port and the parsing of message