当前位置:网站首页>Sword finger offer 06 Print linked list from end to end
Sword finger offer 06 Print linked list from end to end
2022-07-05 08:21:00 【Programmer Xiao Li】
subject :
Enter the head node of a linked list , Return the value of each node from the end to the end ( Return with array ).
Example 1:
Input :head = [1,3,2]
Output :[2,3,1]
Limit :
0 <= Chain length <= 10000
Ideas :
Use the stack to complete the traversal of the linked list , Finally, play the stack in turn to get the results .
/**
* 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;
}
}
边栏推荐
- Weidongshan Internet of things learning lesson 1
- OC and OD gate circuit
- MySQL之MHA高可用集群
- Summary -st2.0 Hall angle estimation
- Problem solving: interpreter error: no file or directory
- Buildroot system for making raspberry pie cm3
- Shell script
- On boost circuit
- H264 (I) i/p/b frame gop/idr/ and other parameters
- Take you to understand the working principle of lithium battery protection board
猜你喜欢
[tutorial 19 of trio basic from introduction to proficiency] detailed introduction of trio as a slave station connecting to the third-party bus (anybus PROFIBUS DP...)
C WinForm [help interface - send email] - practice five
leetcode - 445. 两数相加 II
STM32---IIC
Carrier period, electrical speed, carrier period variation
Shell script
Connection mode - bridge and net
实例005:三数排序 输入三个整数x,y,z,请把这三个数由小到大输出。
H264 (I) i/p/b frame gop/idr/ and other parameters
FIO测试硬盘性能参数和实例详细总结(附源码)
随机推荐
动力电池UL2580测试项目包括哪些
Makefile application
Synchronization of QT multithreading
Measurement fitting based on Halcon learning [II] meaure_ pin. Hdev routine
Shape template matching based on Halcon learning [9] PM_ multiple_ dxf_ models. Hdev routine -- [read and write XLD from DXF file]
Halcon's practice based on shape template matching [2]
Measurement fitting based on Halcon learning [i] fuse Hdev routine
STM32 --- NVIC interrupt
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
My-basic application 2: my-basic installation and operation
Halcon's practice based on shape template matching [1]
leetcode - 445. 两数相加 II
Soem EtherCAT source code analysis attachment 1 (establishment of communication operation environment)
Various types of questions judged by prime numbers within 100 (C language)
Naming rules for FreeRTOS
STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
C language # and #
Slist of linked list
Introduction of air gap, etc
Live555 RTSP audio and video streaming summary (II) modify RTSP server streaming URL address