当前位置:网站首页>剑指 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(); */
}
}
边栏推荐
- Use of room database
- Yolov5 ajouter un mécanisme d'attention
- The next key of win generates the timestamp file of the current day
- Embedded database development programming (VI) -- C API
- When will Wei Lai, who has been watched by public opinion, start to "build high-rise buildings" again?
- Reflection summary of Haut OJ freshmen on Wednesday
- Add level control and logger level control of Solon logging plug-in
- 2022年上半年国家教师资格证考试
- Es module and commonjs learning notes -- ESM and CJS used in nodejs
- On-off and on-off of quality system construction
猜你喜欢
利用HashMap实现简单缓存
Pointnet++的改进
Gbase database helps the development of digital finance in the Bay Area
Double pointer Foundation
质量体系建设之路的分分合合
sync.Mutex源码解读
Magnifying glass effect
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Grail layout and double wing layout
A new micro ORM open source framework
随机推荐
room数据库的使用
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
xftp7与xshell7下载(官网)
sync. Interpretation of mutex source code
ssh免密登录设置及使用脚本进行ssh登录并执行指令
Double pointer Foundation
读者写者模型
Embedded database development programming (V) -- DQL
Simple HelloWorld color change
kubeadm系列-01-preflight究竟有多少check
kubeadm系列-02-kubelet的配置和启动
Magnifying glass effect
Research on the value of background repeat of background tiling
Improvement of pointnet++
软件测试 -- 0 序
Using HashMap to realize simple cache
[interval problem] 435 Non overlapping interval
PMP考试敏捷占比有多少?解疑
Developing desktop applications with electron
YOLOv5添加注意力機制