当前位置:网站首页>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;
}
}
课后习题:
没有啦
边栏推荐
- MySQL----多表查询
- 剑指offer-解决面试题的思路
- 生成随机数
- [Yellow ah code] Introduction to MySQL - 3. I use select, the boss directly drives me to take the train home, and I still buy a station ticket
- 【机器学习】用特征量重要度(feature importance)解释模型靠谱么?怎么才能算出更靠谱的重要度?
- 全国中职网络安全B模块之国赛题远程代码执行渗透测试 PHPstudy的后门漏洞分析
- Flutter Paystack 所有选项实现
- Andoird开发--指南针(基于手机传感器)
- 高并发高可用高性能的解决方案
- 【黄啊码】MySQL入门—3、我用select ,老板直接赶我坐火车回家去,买的还是站票
猜你喜欢
The torch distributed training
文件的逻辑结构与物理结构的对比与区别
postgresql 范围查询比索引查询快吗?
【小程序项目开发--京东商城】uni-app之自定义搜索组件(上)-- 组件UI
I advise those juniors and juniors who have just started working: If you want to enter a big factory, you must master these core skills!Complete Learning Route!
matlab常用符号用法总结
2019 NeurIPS | Graph Convolutional Policy Network for Goal-Directed Molecular Graph Generation
mysql 数据去重的三种方式[实战]
MySQL----多表查询
How on one machine (Windows) to install two MYSQL database
随机推荐
来n遍剑指--05. 替换空格
【RISC-V】risc-v架构学习笔记(架构初学)
数组every和some方法的区别?
【TCP/IP】网络模型
UE4插件软链接(关联)
[MySQL exercises] Chapter 3 Common data types in MySQL
[Cloud native] Introduction and use of Feign of microservices
Flutter Paystack 所有选项实现
【Excel】生成随机数字/字符
ecshop安装的时候提示不支持JPEG格式
编译器R8问题Multidex
@RequestBody和@RequestParam区别
SSM框架简单介绍
如何在一台机器上(windows)安装两个MYSQL数据库
MySQL 高级(进阶) SQL 语句 (一)
SQL join table (inner join, left join, right join, cross join, full outer join)
Flink1.15源码阅读flink-clients——flink命令行帮助命令
哪些字符串会被FastJson解析为null呢
【MySQL功法】第5话 · SQL单表查询
Feign介绍