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

问题解析
1.由于输入输出都是数组,所以新建一个数组,用来存放反转以后的数组即从尾部到头的数组
2.其次取出数组的长度赋值给新数组
3.头结点取值赋值给新数组,然后往后遍历,
代码实例
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution {
public int[] reversePrint(ListNode head) {
ListNode node = head;
int len = 0;
while(node != null){
len++;
node = node.next;//取出数组原始长度
}
int[] nums = new int[len];
while(head != null){
nums[--len] = head.val;//数组从头到尾依次存储每个节点的值
head = head.next;
}
return nums;
}
}
/** * Your CQueue object will be instantiated and called as such: * CQueue obj = new CQueue(); * obj.appendTail(value); * int param_2 = obj.deleteHead(); */
}
}
边栏推荐
- 二十六、文件系统API(设备在应用间的共享;目录和文件API)
- PMP考试敏捷占比有多少?解疑
- [转]MySQL操作实战(一):关键字 & 函数
- 对象的序列化
- xftp7与xshell7下载(官网)
- The next key of win generates the timestamp file of the current day
- Solon 框架如何方便获取每个请求的响应时间?
- Csp-j-2020-excellent split multiple solutions
- Gbase database helps the development of digital finance in the Bay Area
- SAP method of modifying system table data
猜你喜欢

用STM32点个灯

Embedded database development programming (VI) -- C API

读者写者模型
![[转]MySQL操作实战(一):关键字 & 函数](/img/b1/8b843014f365b786e310718f669043.png)
[转]MySQL操作实战(一):关键字 & 函数

Learning notes of "hands on learning in depth"
![To be continued] [UE4 notes] L4 object editing](/img/0f/cfe788f07423222f9eed90f4cece7d.jpg)
To be continued] [UE4 notes] L4 object editing

Optimization scheme of win10 virtual machine cluster

Magnifying glass effect

Binary search basis
![[to be continued] [depth first search] 547 Number of provinces](/img/c4/b4ee3d936776dafc15ac275d2059cd.jpg)
[to be continued] [depth first search] 547 Number of provinces
随机推荐
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
[sum of two numbers] 169 sum of two numbers II - enter an ordered array
How can the Solon framework easily obtain the response time of each request?
JVM call not used once in ten years
Download xftp7 and xshell7 (official website)
Haut OJ 1245: large factorial of CDs --- high precision factorial
[转]:Apache Felix Framework配置属性
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
GBase数据库助力湾区数字金融发展
使用命令符关闭笔记本自带键盘命令
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
Haut OJ 1243: simple mathematical problems
Es module and commonjs learning notes -- ESM and CJS used in nodejs
C语言杂谈1
Reflection summary of Haut OJ freshmen on Wednesday
Haut OJ 1357: lunch question (I) -- high precision multiplication
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
SAP method of modifying system table data
TF-A中的工具介绍
FVP和Juno平台的Memory Layout介绍