当前位置:网站首页>一刷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
边栏推荐
- Mysql database explanation (IX)
- Node. JS ODBC connection PostgreSQL
- 【Leetcode-每日一题】安装栅栏
- Openstack command operation
- 怎么看基金是不是reits,通过银行购买基金安全吗
- Common interview questions of operating system:
- 小程序知识点积累
- 移动app软件测试工具有哪些?第三方软件测评小编分享
- Kubernetes详解(九)——资源配置清单创建Pod实战
- fatal error: torch/extension.h: No such file or directory
猜你喜欢
Detailed explanation of kubernetes (XI) -- label and label selector
网站压测工具Apache-ab,webbench,Apache-Jemeter
G007-HWY-CC-ESTOR-03 华为 Dorado V6 存储仿真器搭建
Machine learning - logistic regression
今日睡眠质量记录76分
cadence SPB17. 4 - Active Class and Subclass
WPS品牌再升级专注国内,另两款国产软件低调出国门,却遭禁令
Sword finger offer (2) -- for Huawei
Mobile finance (for personal use)
c语言---字符串+内存函数
随机推荐
PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder
Go并发和通道
Explanation of redis database (III) redis data type
Common types of automated testing framework ▏ automated testing is handed over to software evaluation institutions
Functions (Part I)
Pytorch中named_parameters、named_children、named_modules函数
携号转网最大赢家是中国电信,为何人们嫌弃中国移动和中国联通?
Sword finger offer (1) -- for Huawei
MySQL sync could not find first log file name in binary log index file error
Hj31 word inversion
MySQL installation process (steps for successful installation)
Example of time complexity calculation
Comparaison du menu de l'illustrateur Adobe en chinois et en anglais
Mumu, go all the way
Krpano panorama vtour folder and tour
Sword finger offer (2) -- for Huawei
Common interview questions of operating system:
【Leetcode-每日一题】安装栅栏
网站压测工具Apache-ab,webbench,Apache-Jemeter
小程序知识点积累