当前位置:网站首页>一刷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
边栏推荐
- Do keyword search, duplicate keyword search, or do not match
- Error: unable to find remote key "17f718f726"“
- Squid agent
- 网站压测工具Apache-ab,webbench,Apache-Jemeter
- Collation of errors encountered in the use of redis shake
- G007-HWY-CC-ESTOR-03 华为 Dorado V6 存储仿真器搭建
- Nacos program connects to mysql8 0+ NullPointerException
- Connect PHP to MSSQL via PDO ODBC
- 服务器中毒了怎么办?服务器怎么防止病毒入侵?
- JVM-第2章-类加载子系统(Class Loader Subsystem)
猜你喜欢
大厂技术实现 | 行业解决方案系列教程
Mysql database explanation (VII)
API gateway / API gateway (III) - use of Kong - current limiting rate limiting (redis)
Mobile finance (for personal use)
Mysql连接查询详解
Analysis of common storage types and FTP active and passive modes
Today's sleep quality record 76 points
移动金融(自用)
Demonstration meeting on startup and implementation scheme of swarm intelligence autonomous operation smart farm project
What exactly does the distributed core principle analysis that fascinates Alibaba P8? I was surprised after reading it
随机推荐
【backtrader源码解析18】yahoo.py 代码注释及解析(枯燥,对代码感兴趣,可以参考)
什么是CNAS认证?CNAS认可的软件测评中心有哪些?
Openstack command operation
Detailed explanation of kubernetes (IX) -- actual combat of creating pod with resource allocation list
Crawling fragment of a button style on a website
pgpool-II 4.3 中文手册 - 入门教程
开源项目推荐:3D点云处理软件ParaView,基于Qt和VTK
cadence SPB17. 4 - Active Class and Subclass
[leetcode daily question] install fence
What exactly does the distributed core principle analysis that fascinates Alibaba P8? I was surprised after reading it
Differential privacy (background)
Grep was unable to redirect to the file
MySQL installation process (steps for successful installation)
Explanation of redis database (III) redis data type
推荐搜索 常用评价指标
What if the package cannot be found
php类与对象
电脑怎么重装系统后显示器没有信号了
c语言---指针进阶
【AI周报】英伟达用AI设计芯片;不完美的Transformer要克服自注意力的理论缺陷