当前位置:网站首页>Sword finger offer question brushing record - offer 06 Print linked list from end to end
Sword finger offer question brushing record - offer 06 Print linked list from end to end
2022-07-19 07:09:00 【When to order】
Enter the head node of a linked list , Return the value of each node from the end to the end ( Return with array ).
Example 1:
Input :head = [1,3,2] Output :[2,3,1]
Method 1: Stack , Reverse the linked list by putting it on the stack and putting it out of the stack
public class Sword_06 {
public static void main(String[] args) {
ListNode l1 = new ListNode(1);
ListNode l2 = new ListNode(3);
ListNode l3 = new ListNode(2);
l1.next = l2;
l2.next = l3;
System.out.println(l1.val);
Sword_06 s6 = new Sword_06();
int[] ints = s6.reversePrint(l1);
for (int i = 0;i < ints.length;i++){
System.out.println(ints[i]);
}
}
public int[] reversePrint(ListNode head) {
Stack<ListNode> stack = new Stack<>();
ListNode temp = head;
while (temp != null){
stack.push(temp);
temp = temp.next;
}
int[] arr = new int[stack.size()];
for (int i = 0;i < arr.length;i++){
ListNode pop = stack.pop();
arr[i] = pop.val;
}
return arr;
}
}
class ListNode {
int val;
ListNode next;
ListNode(int x) { val = x; }
}There is a little error in writing the stack loop , The cycle condition is written at the beginning stack.size(), Ignored after outbound size Reduced problems , So it's always wrong .
Time and space complexity is O(n);
边栏推荐
- 爬虫基础—WEB网页基础
- Regular expression, generator, iterator
- Arm server building my world (MC) version 1.18.2 private server tutorial
- Sword finger offer question brushing record - offer 04 Search in two-dimensional array
- m基于matlab的MIMO信道容量分析,对比了不同天线数量;非码本预编码SVD,GMD;码本预编码DFT,TxAA以及空间分集
- Steam game server configuration selection IP
- 传奇手游怎么开服?需要投资多少?需要那些东西?
- 递归访问目录,打印斐波那契数列,高阶函数
- Minecraft bedrock BDS service tutorial
- M analysis of anti-interference performance of high-speed frequency hopping communication system based on Simulink
猜你喜欢

m基于MATLAB-GUI的GPS数据经纬度高度解析与kalman分析软件设计

Xiaodi network security - Notes (3)

基于小波域的隐马尔可夫树模型的图像去噪方法的matlab实现代码

What role does 5g era server play in this?

Alibaba cloud Hangzhou arm ECS performance evaluation

103.53.124. What is the difference between X IP BGP line and ordinary dedicated line

M simulation of 16QAM and 2DPSK communication links based on Simulink, and get the bit error rate curve by calling Simulink model through MATLAB

数据分析及可视化——京东上销量最高的鞋子

Xiaodi network security - note encryption coding algorithm (6)

m基于matlab的超宽带MIMO雷达对目标的检测仿真,考虑时间反转
随机推荐
闭包与装饰器
Solve the problem that the unit test coverage of sonar will be 0
Sword finger offer question brushing record - offer 03 Duplicate numbers in array
SYN洪水攻击的原理,syn洪水攻击的解决办法
我的世界1.12.2 神奇宝贝(精灵宝可梦) 开服教程
递归访问目录,打印斐波那契数列,高阶函数
正则表达式
wcdma软切换性能matlab仿真m,对比平均激活集数(MASN)、激活集更新率(ASUR)及呼叫中断概率(OP)三个性能指标
Quickly master the sort command and tr command
基于simulink的转速反馈单闭环直流调速系统
103.53.124. What is the difference between X IP BGP line and ordinary dedicated line
Alibaba cloud Hangzhou arm ECS performance evaluation
Pytorch learning diary (II)
我的世界 1.18.1 Forge版 开服教程,可装MOD,带面板
Use of urllib Library
字典、元組和列錶的使用及區別,
103.53.124.X IP段BGP线路和普通的专线有什么区别
Xiaodi - network security notes (1)
Xiaodi network security - Notes (2)
爬虫基础—代理的基本原理