当前位置:网站首页>Come n times - 06. Print the linked list from end to end
Come n times - 06. Print the linked list from end to end
2022-07-31 09:02:00 【Qin Yu】
专栏前言:
本专栏主要是算法训练,目的很简单.在掌握基本的java知识后,Learn the most important algorithm knowledge,Before learning, you must first have a certain understanding of yourself,If you don't know how to do it, welcome to chat privately.
The algorithmic process is boring,But also very special,不断地刷题,The more you share, the better it gets,The only way to truly learn is to explain it to others.Learn knowledge by sharing.
坚持就是胜利~~~
题目描述:
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回).
示例:
输入:head = [1,3,2]
输出:[2,3,1]
限制:
0 <= 链表长度 <= 10000
解题1:
我的比较简单,The idea is to let the pointer go to the end and get the length of the linked list;Create arrays of equal length,The last bit of the array holds the value of the first bit of the linked list,以此类推.得到最终的值
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution {
public int[] reversePrint(ListNode head) {
int num = 0;
ListNode res = head;
while(res != null){
num++;
res = res.next;
}
int result[] = new int[num];
while(head != null){
result[--num] = head.val;
head = head.next;
}
return result;
}
}
解题2:
官方解题思路
栈的特点是后进先出,即最后压入栈的元素最先弹出.考虑到栈的这一特点,使用栈将链表元素顺序倒置.从链表的头节点开始,依次将每个节点压入栈内,然后依次弹出栈内的元素并存储到数组中.
- 创建一个栈,用于存储链表的节点
- 创建一个指针,初始时指向链表的头节点
- 当指针指向的元素非空时,重复下列操作:
- 将指针指向的节点压入栈内
- 将指针移到当前节点的下一个节点
- 获得栈的大小 size,创建一个数组 print,其大小为 size
- 创建下标并初始化 index = 0
- 重复 size 次下列操作:
- 从栈内弹出一个节点,将该节点的值存到 print[index]
- 将 index 的值加 1
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution {
public int[] reversePrint(ListNode head) {
Stack<ListNode> stack = new Stack<ListNode>();
ListNode temp = head;
while (temp != null) {
stack.push(temp);
temp = temp.next;
}
int size = stack.size();
int[] print = new int[size];
for (int i = 0; i < size; i++) {
print[i] = stack.pop().val;
}
return print;
}
}
课后习题:
没有啦

边栏推荐
猜你喜欢

skynet中一条消息从取出到处理完整流程(源码刨析)

如何在一台机器上(windows)安装两个MYSQL数据库

优信年营收16亿:亏损3亿 已与蔚来资本及58集团签署股权协议
![[MySQL exercises] Chapter 3 Common data types in MySQL](/img/11/66b4908ed8f253d599942f35bde96a.png)
[MySQL exercises] Chapter 3 Common data types in MySQL

Flutter Paystack 所有选项实现

免安装版的Mysql安装与配置——详细教程

(selenium)Service geckodriver unexpectedly exited. Status code was: 64

【Unity】编辑器扩展-03-拓展Inspector视图

期刊投递时的 Late News Submission 是什么
![[Cloud native and 5G] Microservices support 5G core network](/img/c9/4ccacd1e70285c2ceb50c324e5018c.png)
[Cloud native and 5G] Microservices support 5G core network
随机推荐
一次Spark SQL线上问题排查和定位
(C语言基础)原样输入输出
Pytorch学习记录(七):自定义模型 & Auto-Encoders
MySQL (2)
MySQL----多表查询
状态机动态规划之股票问题总结
蚂蚁核心科技产品亮相数字中国建设峰会 持续助力企业数字化转型
基于学生成绩管理系统(附源代码及数据库)
编译器R8问题Multidex
奉劝那些刚参加工作的学弟学妹们:要想进大厂,这些核心技能是你必须要掌握的!完整学习路线!
MySQL 高级(进阶) SQL 语句 (一)
【问题记录】TypeError: eval() arg 1 must be a string, bytes or code object
【Unity】编辑器扩展-04-拓展Scene视图
Flutter Paystack 所有选项实现
51单片机-----外部中断
Kotlin 优点
[Mini Program Project Development--Jingdong Mall] Custom Search Component of uni-app (Part 1)--Component UI
来n遍剑指--06. 从尾到头打印链表
MySQL 日期时间类型精确到毫秒
如何在一台机器上(windows)安装两个MYSQL数据库