当前位置:网站首页>从尾到头打印链表
从尾到头打印链表
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;
}
}

边栏推荐
- rk-boot framework combat (1)
- CesiumJS ^ source read [0] 2022 - article directory and source engineering structure
- KDE Frameworks 5.20.0:Plasma迎来诸多改进
- Low dropout linear regulator MPQ2013A-AEC1 brand MPS domestic replacement
- Chinese semantic matching
- 【分层强化学习】survey
- The strongest JVM in the whole network is coming!(Extreme Collector's Edition)
- 绘制几何图形
- 机器人的运动范围
- 定时器学习
猜你喜欢

第一范式、第二范式、第三范式

Worthington Papain & Chymotrypsin & DNase I

消息中间件解析 | 如何正确理解软件应用系统中关于系统通信的那些事?

Worthington解离酶:中性蛋白酶(分散酶)详情解析
![[Cloud native Kubernetes] Build a Kubernetes cluster in binary (middle) - deploy node nodes](/img/07/7f19751f4eadf9fd5612f56d1936d1.png)
[Cloud native Kubernetes] Build a Kubernetes cluster in binary (middle) - deploy node nodes

绘制几何图形

Worthington dissociating enzyme: detailed analysis of neutral protease (dispase)
Docker install MySQL8.0

Since the media increase play a short video?From the three aspects

Worthington Dissociation Enzymes: Trypsin and Frequently Asked Questions
随机推荐
2022/7/29 考试总结
容器化 | 在 Rancher 中部署 MySQL 集群
“灯塔工厂”的中国路径:智造从点到面铺开
“ 我是一名阿里在职9年软件测试工程师,我的经历也许能帮到处于迷茫期的你 ”
what is a .pro file in qt
基于TNEWS‘ 今日头条中文新闻(短文本)分类
月薪15k的阿里测试岗,面试原来这么简单
BEVDetNet:Bird‘s Eye View LiDAR Point Cloud based Real-time 3D Object Detection for Autonomous Drivi
KDE Frameworks 5.20.0: Plasma welcomes many improvements
外包干了五年,废了...
Mysql internal and external connections
Worthington细胞分离技术丨基本原代细胞分离方法和材料
每周推荐短视频:研发效能是什么?它可以实现反“内卷”?
论文精读——YOLOv3: An Incremental Improvement
抖音短视频流量获取攻略,掌握好这些一定可以出爆款
Worthington弹性蛋白酶&透明质酸酶简介
学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
Comprehensive Practice - Three-Mison Chess Mini Game
EA&UML日拱一卒-状态图::重画按钮状态图
The go language (functions, closures, defer, panic/recover, recursion, structure, json serialization and deserialization)