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

边栏推荐
猜你喜欢

EA&UML日拱一卒-多任务编程超入门-(9)线程同步

月薪15k的阿里测试岗,面试原来这么简单

He cell separation technology 丨 basic primary cell separation methods and materials

多商户商城系统功能拆解18讲-平台端商家售后

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

EA&UML日拱一卒-多任务编程超入门-(7)关于mutex,你必须知道的

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

学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难

Worthington弹性蛋白酶&透明质酸酶简介

低压差线性稳压器MPQ2013A-AEC1品牌MPS国产替代
随机推荐
First Normal Form, Second Normal Form, Third Normal Form
leetcode122. Best Time to Buy and Sell Stock II 买卖股票的最佳时机 II(简单)
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
自媒体短视频怎么提高播放量?从这三个方面入手
重写并自定义依赖的原生的Bean方法
"The lighthouse factory" of China path: smart roll out from point to surface
图像的IO操作
Codeforces Round #805 (Div. 3)总结
SQL Server、MySQL主从搭建,EF Core读写分离代码实现
全网最强 JVM 来袭!(至尊典藏版)
Worthington Optimized Technology: Cell Quantification
Go language serialization and deserialization and how to change the uppercase of the json key value to lowercase if the json after serialization is empty
【集训DAY16】ALFA【凸壳】【计算几何】
Override and customize dependent native Bean methods
Music theory & guitar skills
Worthington's tried and tested cell isolation system protocols
kubernets学习 -环境搭建
Since the media increase play a short video?From the three aspects
单片机开发之基本并行I/O口
经典论文-SqueezeNet论文及实践