当前位置:网站首页>Sword finger offer 06. print linked list from end to end
Sword finger offer 06. print linked list from end to end
2022-07-28 10:54:00 【jjj34】
Title Description

Ideas : Use the last in first out feature of stack to realize
1. Create a stack
2. Traversing the linked list , Stack the values corresponding to the linked list nodes
3. Declare an array , The length is the length of the stack , Out of the stack
Knowledge point : Stack , Linked list
Stack related knowledge points : Refer to this blog
Related knowledge points of linked list :

Pictured , The data structure of linked list contains three values ,
The first is an integer , That is, the value corresponding to the node
The second is the pointer , That is to point to the next node ( effect : Connect linked lists )

Pictured , Because of the pointer to the next node , Linked lists can be connected
The end node does not point to an object , The value of the pointer is null
The third is assignment , Assign values to the nodes in the linked list
The code is as follows
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
public int[] reversePrint(ListNode head) {
// Create a stack , Take advantage of the last in first out feature of the stack
// Create a pointer to the head of the linked list
// Create an array , The length is the size of the stack
Stack<Integer> st;
st=new Stack<>();
while(head != null) // The pointer value of the tail node is null To determine whether the linked list is traversed
{
st.push(head.val);
head=head.next;
}
int n =st.size();
int result[] = new int [n];
int i=0;
while(!st.empty())
{
result[i]=st.pop();
i++;
}
return result;
}
}
边栏推荐
- 盘点:令人心动的数据可视化图表
- 11_ UE4 advanced_ Change male characters to female characters and modify the animation
- 使用statement对象执行DDL语句创建表
- 这里有一份超实用Excel快捷键合集(常用+八大类汇总)
- Nodejs:mongodb 简单模糊+分页查询实例
- 分体式测斜探头安装要点及注意事项
- Crm+ zero code: easily realize enterprise informatization
- GKCoherentNoiseSource
- GKPerlinNoiseSource
- 粒子群实现最优解的求解
猜你喜欢

GKCylindersNoiseSource

3. MapReduce explanation and source code analysis

Blue Bridge Cup embedded Hal library systick

Batch Normlization

How to play a ball game with RoboCup 2D

GKConstantNoiseSource

低代码十问:一文讲透关于低代码的一切!

Two years of crud, two graduates, two months of preparation for the interview with ALI, and fortunately won the offer grading p6

SQL Server 2016 learning records - set query

Redis-day01-常识补充及redis介绍
随机推荐
Blue Bridge Cup embedded Hal library ADC
Product side data analysis thinking
GKRidgedNoiseSource
Lucene query syntax memo
OCR knowledge summary
GKConstantNoiseSource
GKRandomSource
Arduino Basics
Solving the optimal solution of particle swarm optimization
GKPolygonObstacle
The blogs of excellent programmers at home and abroad are all here, please check it
GKNoiseMap
蓝桥杯嵌入式-HAL库-LCD
乱打日志的男孩运气怎么样我不知道,加班肯定很多
Crm+ zero code: easily realize enterprise informatization
Attention attention mechanism flow chart
判断数码管是共阳极还是共阴极
Learn how to do e-commerce data analysis (with operation analysis index framework)
GKCircleObstacle
GKVoronoiNoiseSource