当前位置:网站首页>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;
}
}
课后习题:
没有啦

边栏推荐
猜你喜欢

来n遍剑指--05. 替换空格

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

一次Spark SQL线上问题排查和定位

35-Jenkins-共享库应用

matlab常用符号用法总结

SQL join table (inner join, left join, right join, cross join, full outer join)

蚂蚁核心科技产品亮相数字中国建设峰会 持续助力企业数字化转型
![[Mini Program Project Development--Jingdong Mall] Custom Search Component of uni-app (Middle)--Search Suggestions](/img/ea/ee1ad50a497478b9d080bb5e4bdfb5.png)
[Mini Program Project Development--Jingdong Mall] Custom Search Component of uni-app (Middle)--Search Suggestions
![[Cloud native and 5G] Microservices support 5G core network](/img/c9/4ccacd1e70285c2ceb50c324e5018c.png)
[Cloud native and 5G] Microservices support 5G core network

MySQL 视图(详解)
随机推荐
vue element form表单规则校验 点击提交后直接报数据库错误,没有显示错误信息
[MySQL exercises] Chapter 5 · SQL single table query
文件管理:目录管理
求职产品经理【九】求职季,如何写好一份简历?
混合型界面:对话式UI的未来
How to restore data using mysql binlog
SSM框架讲解(史上最详细的文章)
7. JS ES6新增语法 new Map详讲,还有一道代码实战案例帮你快上手new Map
Modular specifications
六、MFC文档类(单文档和多文档)
利用frp服务器进行内网穿透ssh访问
(C语言基础)原样输入输出
(selenium)Service geckodriver unexpectedly exited. Status code was: 64
科目三:右转弯
【pytorch记录】pytorch的分布式 torch.distributed.launch 命令在做什么呢
【Unity】编辑器扩展-02-拓展Hierarchy视图
@RequestBody和@RequestParam区别
SSM framework explanation (the most detailed article in history)
刷题《剑指Offer》day05
5.for in 和 for of区别和使用