当前位置:网站首页>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
边栏推荐
- Wechat applet customer service access to send and receive messages
- 通过 PDO ODBC 将 PHP 连接到 MySQL
- Upgrade MySQL 5.1 to 5.68
- 网站压测工具Apache-ab,webbench,Apache-Jemeter
- What if the package cannot be found
- For examination
- 编译,连接 -- 笔记
- PHP PDO ODBC将一个文件夹的文件装载到MySQL数据库BLOB列,并将BLOB列下载到另一个文件夹
- regular expression
- Upgrade MySQL 5.1 to 5.67
猜你喜欢
随机推荐
控制结构(一)
大型互联网为什么禁止ip直连
开源项目推荐:3D点云处理软件ParaView,基于Qt和VTK
考试考试自用
激活函数的优缺点和选择
Knn,Kmeans和GMM
移动app测试如何进行?
网站某个按钮样式爬取片段
深度学习调参的技巧
时序模型:门控循环单元网络(GRU)
服务器中毒了怎么办?服务器怎么防止病毒入侵?
The El tree implementation only displays a certain level of check boxes and selects radio
推荐搜索 常用评价指标
通过 PDO ODBC 将 PHP 连接到 MSSQL
el-tree实现只显示某一级复选框且单选
c语言---字符串+内存函数
PHP operators
Go语言条件,循环,函数
群体智能自主作业智慧农场项目启动及实施方案论证会议
Wechat applet customer service access to send and receive messages