当前位置:网站首页>One brush 313 sword finger offer 06 Print linked list from end to end (E)
One brush 313 sword finger offer 06 Print linked list from end to end (E)
2022-04-23 15:40:00 【Tang and Song Dynasties】
subject :
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]
Limit :
0 <= Chain length <= 10000
------------------
Ideas :
One way linked list , Want to take value from back to front , Or go through it first and remember lenth Then store in reverse order ,
Or use the stack to temporarily store and then pop up . Always traverse the container twice
The direction is opposite to the original direction , Lenovo stack
Go through... Remember first size Then traverse in sequence But write in reverse order into the array
--------------
/** * 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 index = head;
int count = 0;
while (index != null) {
// Traverse once remember count
index = index.next;
count++;
}
index = head;// Reinitialize index Point to the head node
int[] res = new int[count];// Result set
while (index != null) {
// Traverse
res[--count] = index.val;// Write forward from the end of the array
index = index.next;
}
return res;
}
}
版权声明
本文为[Tang and Song Dynasties]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231535203797.html
边栏推荐
猜你喜欢

c语言---字符串+内存函数

Wechat applet customer service access to send and receive messages

Redis主从复制过程

Mysql database explanation (VII)

Explanation 2 of redis database (redis high availability, persistence and performance management)
![[leetcode daily question] install fence](/img/4e/3ac23174f2b3ee867b45a2e748b872.png)
[leetcode daily question] install fence

字节面试 transformer相关问题 整理复盘

Sorting and replying to questions related to transformer

G007-hwy-cc-estor-03 Huawei Dorado V6 storage simulator construction

T2 icloud calendar cannot be synchronized
随机推荐
Neodynamic Barcode Professional for WPF V11.0
移动金融(自用)
网站压测工具Apache-ab,webbench,Apache-Jemeter
时序模型:门控循环单元网络(GRU)
JSON date time date format
G007-HWY-CC-ESTOR-03 华为 Dorado V6 存储仿真器搭建
一刷312-简单重复set-剑指 Offer 03. 数组中重复的数字(e)
为啥禁用外键约束
Mysql database explanation (IX)
GFS distributed file system (Theory)
多生成树MSTP的配置
一刷314-剑指 Offer 09. 用两个栈实现队列(e)
使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群
【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷
编译,连接 -- 笔记
ICE -- 源码分析
Knn,Kmeans和GMM
Basic concepts of website construction and management
Codejock Suite Pro v20.3.0
怎么看基金是不是reits,通过银行购买基金安全吗