当前位置:网站首页>一刷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
边栏推荐
- 通過 PDO ODBC 將 PHP 連接到 MySQL
- 网站某个按钮样式爬取片段
- Sword finger offer (2) -- for Huawei
- Common types of automated testing framework ▏ automated testing is handed over to software evaluation institutions
- 网站建设与管理的基本概念
- Krpano panorama vtour folder and tour
- T2 icloud calendar cannot be synchronized
- 深度学习调参的技巧
- 编译,连接 -- 笔记
- What exactly does the distributed core principle analysis that fascinates Alibaba P8? I was surprised after reading it
猜你喜欢
随机推荐
c语言---字符串+内存函数
php类与对象
控制结构(一)
Crawling fragment of a button style on a website
小程序知识点积累
Wechat applet customer service access to send and receive messages
Multitimer V2 reconstruction version | an infinitely scalable software timer
Mysql连接查询详解
Example of time complexity calculation
regular expression
Basic concepts of website construction and management
Sorting and replying to questions related to transformer
MySQL InnoDB transaction
X509 certificate cer format to PEM format
Educational codeforces round 127 A-E problem solution
MultiTimer v2 重构版本 | 一款可无限扩展的软件定时器
PHP PDO ODBC loads files from one folder into the blob column of MySQL database and downloads the blob column to another folder
电脑怎么重装系统后显示器没有信号了
Go语言条件,循环,函数
自主作业智慧农场创新论坛








