当前位置:网站首页>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(); */
}
}
边栏推荐
- Haut OJ 1350: choice sends candy
- The number of enclaves
- Palindrome (csp-s-2021-palin) solution
- 剑指 Offer 53 - II. 0~n-1中缺失的数字
- Haut OJ 1357: lunch question (I) -- high precision multiplication
- [sum of two numbers] 169 sum of two numbers II - enter an ordered array
- Es module and commonjs learning notes
- Under the national teacher qualification certificate in the first half of 2022
- kubeadm系列-02-kubelet的配置和启动
- 剑指 Offer 58 - II. 左旋转字符串
猜你喜欢

Little known skills of Task Manager

SAP method of modifying system table data

Fragment addition failed error lookup

Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
![[转]MySQL操作实战(一):关键字 & 函数](/img/b1/8b843014f365b786e310718f669043.png)
[转]MySQL操作实战(一):关键字 & 函数

Shell Sort

质量体系建设之路的分分合合

Double pointer Foundation

Solution to the palindrome string (Luogu p5041 haoi2009)

Chapter 6 data flow modeling - after class exercises
随机推荐
Introduction to tools in TF-A
To the distance we have been looking for -- film review of "flying house journey"
常见的最优化方法
Es module and commonjs learning notes
The present is a gift from heaven -- a film review of the journey of the soul
剑指 Offer 05. 替换空格
[interval problem] 435 Non overlapping interval
Romance of programmers on Valentine's Day
Add level control and logger level control of Solon logging plug-in
记录QT内存泄漏的一种问题和解决方案
[allocation problem] 135 Distribute candy
服务熔断 Hystrix
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
Quick sort summary
A preliminary study of sdei - see the essence through transactions
[sum of two numbers] 169 sum of two numbers II - enter an ordered array
[es practice] use the native realm security mode on es
Improvement of pointnet++
挂起等待锁 vs 自旋锁(两者的使用场合)
[turn to] MySQL operation practice (I): Keywords & functions