当前位置:网站首页>剑指 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(); */
}
}
边栏推荐
- kubeadm系列-02-kubelet的配置和启动
- [allocation problem] 455 Distribute cookies
- 动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
- [es practice] use the native realm security mode on es
- PMP candidates, please check the precautions for PMP examination in July
- Haut OJ 1245: large factorial of CDs --- high precision factorial
- Embedded database development programming (zero)
- Yolov5 adds attention mechanism
- [to be continued] [UE4 notes] L1 create and configure items
- Romance of programmers on Valentine's Day
猜你喜欢
随机推荐
FVP和Juno平台的Memory Layout介绍
Gbase database helps the development of digital finance in the Bay Area
Demonstration of using Solon auth authentication framework (simpler authentication framework)
Optimization scheme of win10 virtual machine cluster
To be continued] [UE4 notes] L4 object editing
Fragment addition failed error lookup
Haut OJ 1352: string of choice
SAP-修改系统表数据的方法
小程序直播+电商,想做新零售电商就用它吧!
TF-A中的工具介绍
2022年上半年国家教师资格证考试
用STM32点个灯
2022上半年全国教师资格证下
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
C language Essay 1
Introduction to memory layout of FVP and Juno platforms
Software test -- 0 sequence
[turn]: Apache Felix framework configuration properties
SAP method of modifying system table data
Improvement of pointnet++