当前位置:网站首页>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(); */
}
}
边栏推荐
- Mysql database (I)
- Software test -- 0 sequence
- A new micro ORM open source framework
- Insert sort
- [binary search] 34 Find the first and last positions of elements in a sorted array
- YOLOv5-Shufflenetv2
- Romance of programmers on Valentine's Day
- sync. Interpretation of mutex source code
- Haut OJ 1241: League activities of class XXX
- [to be continued] I believe that everyone has the right to choose their own way of life - written in front of the art column
猜你喜欢
剑指 Offer 09. 用两个栈实现队列
To the distance we have been looking for -- film review of "flying house journey"
Fragment addition failed error lookup
剑指 Offer 05. 替换空格
GBase数据库助力湾区数字金融发展
Support multi-mode polymorphic gbase 8C database continuous innovation and heavy upgrade
YOLOv5-Shufflenetv2
Yolov5 ajouter un mécanisme d'attention
剑指 Offer 35.复杂链表的复制
Talking about JVM (frequent interview)
随机推荐
[turn]: Apache Felix framework configuration properties
Chapter 6 data flow modeling - after class exercises
Haut OJ 1350: choice sends candy
用STM32点个灯
Codeforces round 712 (Div. 2) d. 3-coloring (construction)
Pointnet++的改进
SAP method of modifying system table data
TF-A中的工具介绍
YOLOv5添加注意力機制
[depth first search] 695 Maximum area of the island
发现一个很好的 Solon 框架试手的教学视频(Solon,轻量级应用开发框架)
Detailed explanation of expression (csp-j 2021 expr) topic
[转]MySQL操作实战(三):表联结
Download xftp7 and xshell7 (official website)
High precision subtraction
剑指 Offer 58 - II. 左旋转字符串
常见的最优化方法
Summary of Haut OJ 2021 freshman week
Corridor and bridge distribution (csp-s-2021-t1) popular problem solution
[turn to] MySQL operation practice (III): table connection