当前位置:网站首页>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;
}
}
边栏推荐
- [trio basic tutorial 17 from getting started to mastering] set up and connect the trio motion controller and input the activation code
- Halcon's practice based on shape template matching [2]
- Bluebridge cup internet of things basic graphic tutorial - GPIO output control LD5 on and off
- H264 (I) i/p/b frame gop/idr/ and other parameters
- [cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
- Stm32--- systick timer
- Class of color image processing based on Halcon learning_ ndim_ norm. hdev
- Explain task scheduling based on Cortex-M3 in detail (Part 2)
- STM32 summary (HAL Library) - DHT11 temperature sensor (intelligent safety assisted driving system)
- VESC Benjamin test motor parameters
猜你喜欢
NTC thermistor application - temperature measurement
[tutorial 15 of trio basic from introduction to proficiency] trio free serial communication
On boost circuit
C WinForm [display real-time time in the status bar] - practical exercise 1
C WinForm [help interface - send email] - practice five
Some thoughts on extracting perspectives from ealfa and Ebeta
C WinForm [view status bar -- statusstrip] - Practice 2
Stm32--- systick timer
Talk about the circuit use of TVs tube
[paper reading] the latest transfer ability in deep learning: a survey in 2022
随机推荐
QEMU STM32 vscode debugging environment configuration
Take you to understand the working principle of lithium battery protection board
Bluetooth hc-05 pairing process and precautions
Basic embedded concepts
Various types of questions judged by prime numbers within 100 (C language)
Talk about the function of magnetic beads in circuits
C WinForm [help interface - send email] - practice five
Introduction of air gap, etc
[trio basic tutorial 16 from introduction to proficiency] UDP communication test supplement
C WinForm [display real-time time in the status bar] - practical exercise 1
On boost circuit
STM32 --- serial port communication
Keil use details -- magic wand
Relationship between line voltage and phase voltage, line current and phase current
[cloud native | learn kubernetes from scratch] III. kubernetes cluster management tool kubectl
Zero length array in GNU C
Stm32--- systick timer
Circleq of linked list
How to copy formatted notepad++ text?
Classic application of MOS transistor circuit design (1) -iic bidirectional level shift