当前位置:网站首页>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
边栏推荐
- MySQL query library size
- Rsync + inotify remote synchronization
- KNN, kmeans and GMM
- 什么是CNAS认证?CNAS认可的软件测评中心有哪些?
- Leetcode学习计划之动态规划入门day3(198,213,740)
- Mysql database explanation (8)
- Educational codeforces round 127 A-E problem solution
- shell脚本中的DATE日期计算
- Pytorch中named_parameters、named_children、named_modules函数
- Why disable foreign key constraints
猜你喜欢
随机推荐
Connectez PHP à MySQL via aodbc
移动app软件测试工具有哪些?第三方软件测评小编分享
Why disable foreign key constraints
使用 Bitnami PostgreSQL Docker 镜像快速设置流复制集群
MultiTimer v2 重构版本 | 一款可无限扩展的软件定时器
Detailed explanation of MySQL connection query
regular expression
小程序知识点积累
通過 PDO ODBC 將 PHP 連接到 MySQL
pgpool-II 4.3 中文手册 - 入门教程
移动金融(自用)
What is CNAs certification? What are the software evaluation centers recognized by CNAs?
PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder
字符串最后一个单词的长度
Upgrade MySQL 5.1 to 5.69
Wechat applet customer service access to send and receive messages
推荐搜索 常用评价指标
Explanation 2 of redis database (redis high availability, persistence and performance management)
激活函数的优缺点和选择
怎么看基金是不是reits,通过银行购买基金安全吗