当前位置:网站首页>剑指 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(); */
}
}
边栏推荐
- Software test -- 0 sequence
- 质量体系建设之路的分分合合
- SDEI初探-透过事务看本质
- SAP method of modifying system table data
- Fragment addition failed error lookup
- JVM call not used once in ten years
- [转]:Apache Felix Framework配置属性
- Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
- Shell Sort
- Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
猜你喜欢

Bucket sort

Research on the value of background repeat of background tiling

Using HashMap to realize simple cache

lxml.etree.XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8

利用HashMap实现简单缓存

Chapter 6 data flow modeling - after class exercises

Research on the value of background repeat of background tiling

Page countdown

Acwing 4300. Two operations

Reader writer model
随机推荐
[trans]: spécification osgi
PMP考生,请查收7月PMP考试注意事项
浅谈JVM(面试常考)
Bubble sort summary
High precision subtraction
Pointnet++学习
[interval problem] 435 Non overlapping interval
kubeadm系列-02-kubelet的配置和启动
Zheng Qing 21 ACM is fun. (3) part of the problem solution and summary
Service fusing hystrix
Programmers' experience of delivering takeout
26、 File system API (device sharing between applications; directory and file API)
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
YOLOv5添加注意力机制
[to be continued] [UE4 notes] L3 import resources and project migration
Solon Logging 插件的添加器级别控制和日志器的级别控制
2022年上半年国家教师资格证考试
Es module and commonjs learning notes
支持多模多态 GBase 8c数据库持续创新重磅升级
[to be continued] [depth first search] 547 Number of provinces