当前位置:网站首页>剑指 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(); */
}
}
边栏推荐
- Haut OJ 1243: simple mathematical problems
- [binary search] 69 Square root of X
- To the distance we have been looking for -- film review of "flying house journey"
- kubeadm系列-02-kubelet的配置和启动
- [turn to] MySQL operation practice (III): table connection
- Pointnet++学习
- PMP candidates, please check the precautions for PMP examination in July
- Research on the value of background repeat of background tiling
- C language Essay 1
- Add level control and logger level control of Solon logging plug-in
猜你喜欢
Hang wait lock vs spin lock (where both are used)
Embedded database development programming (VI) -- C API
支持多模多态 GBase 8c数据库持续创新重磅升级
Count sort
[to be continued] [UE4 notes] L3 import resources and project migration
Simple modal box
Web APIs DOM节点
Yolov5 adds attention mechanism
小程序直播+电商,想做新零售电商就用它吧!
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
随机推荐
小程序直播+电商,想做新零售电商就用它吧!
[转]MySQL操作实战(三):表联结
YOLOv5添加注意力机制
[转]: OSGI规范 深入浅出
A three-dimensional button
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research
26、 File system API (device sharing between applications; directory and file API)
[轉]: OSGI規範 深入淺出
Research on the value of background repeat of background tiling
Embedded database development programming (V) -- DQL
Chapter 6 data flow modeling - after class exercises
Double pointer Foundation
Haut OJ 1245: large factorial of CDs --- high precision factorial
Haut OJ 1241: League activities of class XXX
2022年上半年国家教师资格证考试
利用HashMap实现简单缓存
Service fusing hystrix
lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
room数据库的使用