当前位置:网站首页>从尾到头打印链表
从尾到头打印链表
2022-07-30 00:09:00 【龙崎流河】
题目:
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)
示例一:
输入:head = [1,3,2]
输出:[2,3,1]
分析:
先想着创建一个数组,首先需要确定有几个数字,确定完后就可以直接装填了。
代码:
public class ReversePrint {
public int[] reversePrint(ListNode head) {
if (head == null){
return new int[0];
}
ListNode temp = head;
int count = 0;
//计算返回数组长度
while (temp != null){
count++;
temp = temp.next;
}
int k = count - 1;
int[] res = new int[count];
//可以开始赋值了
while (head !=null){
res[k--] = head.val;
head = head.next;
}
return res;
}
}

边栏推荐
- Worthington Dissociation Enzymes: Collagenase and Four Basic Profiles
- EA&UML日拱一卒-状态图::重画按钮状态图
- Adaptive feature fusion pyramid network for multi-classes agriculturalpest detection
- Genesis与Axis Ventures互动密切
- The basic parallel I/O port of single chip microcomputer development
- First Normal Form, Second Normal Form, Third Normal Form
- 管理区解耦架构见过吗?能帮客户解决大难题的
- vim相关介绍(三)
- NumPy(二)
- UE4 制作十字准心+后坐力
猜你喜欢

『牛客|每日一题』走迷宫

BEVDetNet: Bird's Eye View LiDAR Point Cloud based Real-time 3D Object Detection for Autonomous Drivi

Worthington细胞分离技术丨基本原代细胞分离方法和材料

From the perspective: the interviewer interview function test engineer mainly inspects what ability?

Paper Intensive Reading - YOLOv3: An Incremental Improvement

vim相关介绍(三)

外包干了五年,废了...

Worthington酶促细胞收获&细胞粘附和收获

BEVDetNet:Bird‘s Eye View LiDAR Point Cloud based Real-time 3D Object Detection for Autonomous Drivi

Some personal understandings about MySQL indexes (partially refer to MySQL45 lectures)
随机推荐
EA&UML日拱一卒-状态图::重画按钮状态图
Add, delete, modify and query the database
Codeforces Round #805 (Div. 3)总结
Worthington酶促细胞收获&细胞粘附和收获
基于TNEWS‘ 今日头条中文新闻(短文本)分类
【分层强化学习】HAC源码解读
opencv基本图像的滤波
2022/7/29 考试总结
SQL Server、MySQL主从搭建,EF Core读写分离代码实现
Worthington Papain & Chymotrypsin & DNase I
ZLMediaKit源码学习——UDP
C陷阱与缺陷 第5章 库函数 5.1 返回整数的getchar函数
Since the media how to write a short video title?Three hot style title, let your video gain more traffic
what is a .pro file in qt
rk-boot framework combat (1)
容器化 | 在 Rancher 中部署 MySQL 集群
Reading notes. This is the psychology: see through the essence of the pseudo psychology (version 10)"
[Cloud native Kubernetes] Build a Kubernetes cluster in binary (middle) - deploy node nodes
18 Lectures on Disassembly of Multi-merchant Mall System Functions
what is a .pro file in qt