当前位置:网站首页>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(); */
}
}
边栏推荐
- Maximum number of "balloons"
- YOLOv5添加注意力機制
- 使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
- After setting up the database and website When you open the app for testing, it shows that the server is being maintained
- Development error notes
- 支持多模多态 GBase 8c数据库持续创新重磅升级
- kubeadm系列-00-overview
- 卷积神经网络——卷积层
- 一个新的微型ORM开源框架
- 动漫评分数据分析与可视化 与 IT行业招聘数据分析与可视化
猜你喜欢
Pointnet++的改进
Hang wait lock vs spin lock (where both are used)
National teacher qualification examination in the first half of 2022
[depth first search] 695 Maximum area of the island
[turn to] MySQL operation practice (I): Keywords & functions
Little known skills of Task Manager
object serialization
Romance of programmers on Valentine's Day
游戏商城毕业设计
Quick sort summary
随机推荐
Summary of Haut OJ 2021 freshman week
Haut OJ 1245: large factorial of CDs --- high precision factorial
Haut OJ 1401: praise energy
Palindrome (csp-s-2021-palin) solution
sync.Mutex源码解读
Download xftp7 and xshell7 (official website)
FVP和Juno平台的Memory Layout介绍
Haut OJ 1347: addition of choice -- high progress addition
二十六、文件系统API(设备在应用间的共享;目录和文件API)
A problem and solution of recording QT memory leakage
The number of enclaves
注解与反射
Pointnet++ learning
使用Room数据库报警告: Schema export directory is not provided to the annotation processor so we cannot expor
Shell Sort
What is the agile proportion of PMP Exam? Dispel doubts
[speed pointer] 142 circular linked list II
[allocation problem] 135 Distribute candy
剑指 Offer 04. 二维数组中的查找
Hang wait lock vs spin lock (where both are used)