当前位置:网站首页>剑指 Offer 06. 从尾到头打印链表
剑指 Offer 06. 从尾到头打印链表
2022-07-28 10:28:00 【jjj34】
题目描述

思路:利用栈的后进先出的特性来实现
1.创建栈
2.遍历链表,将链表节点对应的值入栈
3.声明一个数组,长度为栈的长度,出栈
知识点:栈,链表
栈的相关知识点:参考这篇博客
剑指 Offer 09. 用两个栈实现队列_jcxj2934的博客-CSDN博客
链表的相关知识点:

如图,链表这个数据结构中包含了三个值,
第一个是整数, 即节点对应的值
第二个是指针,即指向下一个节点(作用:将链表连接起来)

如图,正因为有了指向下一个节点的指针,链表才能连接起来
末尾节点没有指向的对象,指针的值为null
第三个是赋值,给链表中的节点赋值
代码如下
/**
* 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<Integer> st;
st=new Stack<>();
while(head != null) //利用尾节点的指针值为null来判断链表是否全部遍历
{
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;
}
}
边栏推荐
- GKNoiseMap
- 6. MapReduce custom partition implementation
- markdown转成word或者pdf
- QT generation Exe file and run without QT environment (enigma virtual box for green executable software packaging) graphic tutorial
- GKBillowNoiseSource
- GKObstacle
- 10_ue4进阶_添加倒地和施法动作
- 哈希表的相关知识点
- Select without the order by clause, the order of the returned results is not reliable
- Install Office customization. Troubleshooting during installation
猜你喜欢

7、MapReduce自定义排序实现

Django celery redis send email asynchronously
![[application of stack] - infix expression to suffix expression](/img/c1/879716342f6dd5eaa8b79c752eca16.png)
[application of stack] - infix expression to suffix expression

蓝桥杯嵌入式-HAL库-SYSTICK

蓝桥杯嵌入式-HAL库-USART_RX

Blue Bridge Cup embedded Hal library USART_ TX

How to play a ball game with RoboCup 2D

5、Window端实现Mapreduce程序完成wordcount功能

Excel word 简单 技巧 整理(持续更新 大概~)

Two years of crud, two graduates, two months of preparation for the interview with ALI, and fortunately won the offer grading p6
随机推荐
GKRandomSource
nodemcu之开发环境配置
GKSphereObstacle
图片滑动特效
clo*******e:项目管理随记
Test question discovery ring of previous test questions
I don't know how lucky the boy who randomly typed logs is. There must be a lot of overtime
Development environment configuration of nodemcu
Andorid development
Go json. Decoder Considered Harmful
GKVoronoiNoiseSource
Redis-day01-常识补充及redis介绍
GKObstacle
Install Office customization. Troubleshooting during installation
SQL Server 2016 learning records - single table query
Pyqt5 rapid development and practice 4.11 drag and clipboard
GKRandom
Solving the optimal solution of particle swarm optimization
GKLinearCongruentialRandomSource
数组相关的知识点