当前位置:网站首页>一刷313-剑指 Offer 06. 从尾到头打印链表(e)
一刷313-剑指 Offer 06. 从尾到头打印链表(e)
2022-04-23 15:35:00 【丿唐宋】
题目:
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
------------
示例 1:
输入:head = [1,3,2]
输出:[2,3,1]
限制:
0 <= 链表长度 <= 10000
------------------
思路:
单向链表,想要从后往前取值,要么先遍历一次记住lenth再倒序存储,
要么用栈临时存储再弹出。始终都需要遍历两次容器
方向与原来方向相反,联想栈
先遍历一次记住size 再顺序遍历 但是倒序写入数组中
--------------
/** * 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) {
//遍历一次 记住count
index = index.next;
count++;
}
index = head;//再初始化 index指向头结点
int[] res = new int[count];//结果集
while (index != null) {
//遍历
res[--count] = index.val;//从数组尾部向前写入
index = index.next;
}
return res;
}
}
版权声明
本文为[丿唐宋]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_45170946/article/details/124359403
边栏推荐
- Knn,Kmeans和GMM
- PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder
- 2022年中国数字科技专题分析
- 服务器中毒了怎么办?服务器怎么防止病毒入侵?
- Cookie&Session
- Byte interview programming question: the minimum number of K
- Machine learning - logistic regression
- Common types of automated testing framework ▏ automated testing is handed over to software evaluation institutions
- Do keyword search, duplicate keyword search, or do not match
- Comparaison du menu de l'illustrateur Adobe en chinois et en anglais
猜你喜欢
Deep learning - Super parameter setting
函数(第一部分)
T2 iCloud日历无法同步
Detailed explanation of kubernetes (XI) -- label and label selector
For examination
移动金融(自用)
Basic concepts of website construction and management
Mysql database explanation (VII)
2022年中国数字科技专题分析
Detailed explanation of kubernetes (IX) -- actual combat of creating pod with resource allocation list
随机推荐
考试考试自用
携号转网最大赢家是中国电信,为何人们嫌弃中国移动和中国联通?
Pytorch中named_parameters、named_children、named_modules函数
kubernetes之常用Pod控制器的使用
T2 icloud calendar cannot be synchronized
Do keyword search, duplicate keyword search, or do not match
深度学习调参的技巧
Mysql database explanation (IX)
如果conda找不到想要安装的库怎么办PackagesNotFoundError: The following packages are not available from current
Connect PHP to MSSQL via PDO ODBC
JSON date time date format
fatal error: torch/extension. h: No such file or directory
Cookie&Session
MySQL Basics
Multitimer V2 reconstruction version | an infinitely scalable software timer
推荐搜索 常用评价指标
【递归之数的拆分】n分k,限定范围的拆分
群体智能自主作业智慧农场项目启动及实施方案论证会议
Comparaison du menu de l'illustrateur Adobe en chinois et en anglais
大厂技术实现 | 行业解决方案系列教程