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

边栏推荐
- Minesweeper game in c language
- Expansion of Parallel I/O Port in Single Chip Microcomputer Development
- 关于 byte 的范围
- 对数据库进行增删改查操作
- c语言小游戏扫雷
- 自媒体短视频怎么提高播放量?从这三个方面入手
- Codeforces Round #805 (Div. 3)总结
- C陷阱与缺陷 第4章 链接 4.3 命名冲突与static修饰符
- Some personal understandings about MySQL indexes (partially refer to MySQL45 lectures)
- ApplicationContext的三大实现
猜你喜欢

Douyin short video traffic acquisition strategy, mastering these will definitely be a hit

【分层强化学习】survey

The strongest JVM in the whole network is coming!(Extreme Collector's Edition)

关于MySQL索引的一些个人理解(部分参考MySQL45讲)

【集训DAY18】有趣的交换【模拟】【数学】

更换可执行文件glibc版本的某一次挣扎

Introduction to Worthington Elastase & Hyaluronidase

关于 byte 的范围

vim相关介绍(三)

管理区解耦架构见过吗?能帮客户解决大难题的
随机推荐
what is a .pro file in qt
Worthington木瓜蛋白酶&胰凝乳蛋白酶&脱氧核糖核酸酶 I
He cell separation technology 丨 basic primary cell separation methods and materials
First Normal Form, Second Normal Form, Third Normal Form
CesiumJS ^ source read [0] 2022 - article directory and source engineering structure
指令集数据产品如何设计和实现报表协同系统——基于指令集物联网操作系统的工业协同制造项目开发实践
绘制几何图形
jenkins搭建部署详细步骤
Worthington细胞分离技术丨基本原代细胞分离方法和材料
【分层强化学习】HAC源码解读
ZLMediaKit源码学习——UDP
『牛客|每日一题』走迷宫
Worthington经过使用测试的细胞分离系统方案
Getting Started with Sentinel
kubernets学习 -环境搭建
【集训DAY16】KC ‘ s Stars【dfs】
C陷阱与缺陷 第4章 链接 4.2 声明与定义
18 Lectures on Disassembly of Multi-merchant Mall System Functions
C陷阱与缺陷 第4章 链接 4.3 命名冲突与static修饰符
UE4 makes crosshair + recoil