当前位置:网站首页>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;
}
}
边栏推荐
- Bootloader implementation of PIC MCU
- Imx6ull bare metal development learning 2- use C language to light LED indicator
- Working principle and type selection of common mode inductor
- General makefile (I) single C language compilation template
- Introduction of air gap, etc
- 实例008:九九乘法表
- MHA High available Cluster for MySQL
- Shell script realizes the reading of serial port and the parsing of message
- leetcode - 445. 两数相加 II
- [paper reading] the latest transfer ability in deep learning: a survey in 2022
猜你喜欢
[trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
H264 (I) i/p/b frame gop/idr/ and other parameters
NTC thermistor application - temperature measurement
实例006:斐波那契数列
Simple design description of MIC circuit of ECM mobile phone
Shell script
STM32 single chip microcomputer - bit band operation
QEMU STM32 vscode debugging environment configuration
Volatile of C language
Relationship between line voltage and phase voltage, line current and phase current
随机推荐
实例001:数字组合 有四个数字:1、2、3、4,能组成多少个互不相同且无重复数字的三位数?各是多少?
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
Several implementation schemes of anti reverse connection protection of positive and negative poles of power supply!
Detailed explanation of SQL server stored procedures
MySQL之MHA高可用集群
Stm32--- systick timer
Halcon's practice based on shape template matching [1]
STM32 --- configuration of external interrupt
STM32 single chip microcomputer - external interrupt
[trio basic tutorial 18 from introduction to proficiency] trio motion controller UDP fast exchange data communication
Shape template matching based on Halcon learning [vi] find_ mirror_ dies. Hdev routine
Hardware 3 -- function of voltage follower
Live555 push RTSP audio and video stream summary (III) flower screen problem caused by pushing H264 real-time stream
Bluetooth hc-05 pairing process and precautions
STM32 single chip microcomputer -- volatile keyword
Imx6ull bare metal development learning 2- use C language to light LED indicator
Buildroot system for making raspberry pie cm3
STM32 --- serial port communication
C WinForm [get file path -- traverse folder pictures] - practical exercise 6
STM32---IIC