当前位置:网站首页>Sword finger offer 06 Print linked list from beginning to end
Sword finger offer 06 Print linked list from beginning to end
2022-07-05 05:28:00 【ThE wAlkIng D】
Title Description
Problem analysis
1. Because the input and output are arrays , So create a new array , It is used to store the array after inversion, that is, the array from tail to head
2. Next, take out the length of the array and assign it to the new array
3. The value of the header node is assigned to the new array , Then go back ,
Code instance
/** * 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;// Fetch the original length of the array
}
int[] nums = new int[len];
while(head != null){
nums[--len] = head.val;// The array stores the values of each node from beginning to end
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(); */
}
}
边栏推荐
- Solon Logging 插件的添加器级别控制和日志器的级别控制
- Yolov5 adds attention mechanism
- 软件测试 -- 0 序
- [turn]: Apache Felix framework configuration properties
- 剑指 Offer 58 - II. 左旋转字符串
- The number of enclaves
- lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- Chapter 6 data flow modeling - after class exercises
- Heap sort summary
- 远程升级怕截胡?详解FOTA安全升级
猜你喜欢
[turn to] MySQL operation practice (III): table connection
Count sort
Web APIs DOM节点
A new micro ORM open source framework
[turn to] MySQL operation practice (I): Keywords & functions
TF-A中的工具介绍
Using HashMap to realize simple cache
Pointnet++学习
[to be continued] [UE4 notes] L2 interface introduction
服务熔断 Hystrix
随机推荐
A misunderstanding about the console window
Haut OJ 1221: a tired day
Chapter 6 data flow modeling - after class exercises
动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
Summary of Haut OJ 2021 freshman week
Haut OJ 1347: addition of choice -- high progress addition
TF-A中的工具介绍
服务熔断 Hystrix
软件测试 -- 0 序
On-off and on-off of quality system construction
YOLOv5添加注意力機制
2022上半年全国教师资格证下
剑指 Offer 35.复杂链表的复制
注解与反射
To be continued] [UE4 notes] L4 object editing
Fragment addition failed error lookup
The present is a gift from heaven -- a film review of the journey of the soul
Haut OJ 1357: lunch question (I) -- high precision multiplication
每日一题-搜索二维矩阵ps二维数组的查找
Reflection summary of Haut OJ freshmen on Wednesday