当前位置:网站首页>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
边栏推荐
- 删除字符串中出现次数最少的字符
- 携号转网最大赢家是中国电信,为何人们嫌弃中国移动和中国联通?
- Functions (Part I)
- MySQL集群模式与应用场景
- T2 icloud calendar cannot be synchronized
- Codejock Suite Pro v20. three
- cadence SPB17.4 - Active Class and Subclass
- G007-HWY-CC-ESTOR-03 华为 Dorado V6 存储仿真器搭建
- Pytorch中named_parameters、named_children、named_modules函数
- 怎么看基金是不是reits,通过银行购买基金安全吗
猜你喜欢
Explanation 2 of redis database (redis high availability, persistence and performance management)
Advantages, disadvantages and selection of activation function
Codejock Suite Pro v20.3.0
Why is IP direct connection prohibited in large-scale Internet
Application of Bloom filter in 100 million flow e-commerce system
[leetcode daily question] install fence
木木一路走好呀
Detailed explanation of MySQL connection query
Mysql database explanation (8)
单体架构系统重新架构
随机推荐
Mumu, go all the way
计算某字符出现次数
Basic concepts of website construction and management
幂等性的处理
G007-hwy-cc-estor-03 Huawei Dorado V6 storage simulator construction
负载均衡器
How to test mobile app?
[leetcode daily question] install fence
ICE -- 源码分析
G007-HWY-CC-ESTOR-03 华为 Dorado V6 存储仿真器搭建
码住收藏▏软件测试报告模板范文来了
IronPDF for . NET 2022.4.5455
shell脚本中的DATE日期计算
通过 PDO ODBC 将 PHP 连接到 MSSQL
推荐搜索 常用评价指标
Detailed explanation of MySQL connection query
Connect PHP to MSSQL via PDO ODBC
Mysql database explanation (8)
Why disable foreign key constraints
Node. JS ODBC connection PostgreSQL