【三种解法】剑指 Offer 06. 从尾到头打印链表【附完整可运行代码】
立志用最少的代码做最高效的表达
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
示例 1:
输入:head = [1,3,2]
输出:[2,3,1]
限制:
0 <= 链表长度 <= 10000
文章目录
- 解法1:栈
- 解法2
- 解法3
- 完整可运行代码
- 测试用例
- 本题考点
解法1:栈
利用栈先进后出的原理,将链表遍历并存入栈,新建数组,将栈依次弹出,
得到的就是倒序的数组。
本来遍历一遍就出结果的题,我也不知为啥脑子抽了要用栈。。
class Solution6 {public int[] reversePrint(ListNode head) {// 避免出现空指针if(head == null) return new int[0];List<Integer> array = new ArrayList<Integer>();int num = 0;Stack<Integer> st = new Stack<Integer>();while(true) {st.push(head.val);head = head.next;if(head == null) {break;}}while(!st.empty()) {num++;array.add(st.peek());st.pop();}return array.stream().mapToInt(Integer::valueOf).toArray();}
}
解法2
直接使用ArrayList动态数组,将栈值依次放入数组,然后遍历数组,将数组置换,或使用reverse()。最后转化为int[]即可。
class Solution7 {public int[] reversePrint(ListNode head) {// 避免出现空指针if(head == null) return new int[0];List<Integer> list = new ArrayList<Integer>();while(true) {list.add(head.val);head = head.next;if(head == null) break;}int len = list.size()-1;int num = 0;while(num < len) {int t = list.get(num);list.set(num, list.get(len));list.set(len, t);num++; len--;}return list.stream().mapToInt(Integer::valueOf).toArray();}
}
解法3
先进行一遍遍历,将链表长度求出,然后定义对应长度的int数组,最后倒序赋值,返回即可。
class Solution8 {public int[] reversePrint(ListNode head) {// 避免出现空指针if(head == null) return new int[0];int count = 0;ListNode tmp_head = head;while(tmp_head != null) {count++;tmp_head = tmp_head.next;}int array[] = new int[count];for(int i = count-1; i >= 0; i--) {array[i] = head.val;head = head.next;}return array;}
}
完整可运行代码
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;
import java.util.Vector;public class 剑指_Offer_06_从尾到头打印链表 {public static void main(String[] args) {// 声明链表,这里的Solution6、7、8分别代表三种解法。Solution8 solution = new Solution8();ListNode head = new ListNode(1);ListNode n1 = new ListNode(2);ListNode n3 = new ListNode(3);ListNode n4 = new ListNode(4);ListNode n5 = new ListNode(5);n4.next = n5;n3.next = n4;n1.next = n3;head.next = n1;// 输出数组int node[] = solution.reversePrint(head);for(int i = 0; i < node.length; i++) {System.out.println(node[i] + " ");}}
}class ListNode {int val;ListNode next;ListNode(int x) { val = x; }
}// 解法1:栈
// 利用栈先进后出的原理,将链表遍历并存入栈,新建数组,将栈依次弹出,
// 得到的就是倒序的数组。
// 本来遍历一遍就出结果的题,我也不知为啥脑子抽了要用栈。。
class Solution6 {public int[] reversePrint(ListNode head) {// 避免出现空指针if(head == null) return new int[0];List<Integer> array = new ArrayList<Integer>();int num = 0;Stack<Integer> st = new Stack<Integer>();while(true) {st.push(head.val);head = head.next;if(head == null) {break;}}while(!st.empty()) {num++;array.add(st.peek());st.pop();}return array.stream().mapToInt(Integer::valueOf).toArray();}
}// 解法2,直接使用ArrayList动态数组,最后转化为int即可。
class Solution7 {public int[] reversePrint(ListNode head) {// 避免出现空指针if(head == null) return new int[0];List<Integer> list = new ArrayList<Integer>();while(true) {list.add(head.val);head = head.next;if(head == null) break;}int len = list.size()-1;int num = 0;while(num < len) {int t = list.get(num);list.set(num, list.get(len));list.set(len, t);num++; len--;}return list.stream().mapToInt(Integer::valueOf).toArray();}
}// 解法3,先进行一遍遍历,将链表长度求出,然后定义对应长度的int数组,最后倒序赋值,返回即可。
class Solution8 {public int[] reversePrint(ListNode head) {// 避免出现空指针if(head == null) return new int[0];int count = 0;ListNode tmp_head = head;while(tmp_head != null) {count++;tmp_head = tmp_head.next;}int array[] = new int[count];for(int i = count-1; i >= 0; i--) {array[i] = head.val;head = head.next;}return array;}
}
测试用例
- 功能测试:输入的链表有多个节点;输入的链表只有一个节点
- 特殊输入测试:输入的链表头结点指针为null
本题考点
- 考查应聘者对单向链表的理解和编程能力
- 考查应聘者对循环、递归和栈3个相互关联的概念的理解。
【三种解法】剑指 Offer 06. 从尾到头打印链表【附完整可运行代码】相关推荐
- 【LeetCode】剑指 Offer 06. 从尾到头打印链表
[LeetCode]剑指 Offer 06. 从尾到头打印链表 文章目录 [LeetCode]剑指 Offer 06. 从尾到头打印链表 一.笨比解法 二.递归法 三.辅助栈法 总结 一.笨比解法 算 ...
- 《LeetCode力扣练习》剑指 Offer 06. 从尾到头打印链表 Java
<LeetCode力扣练习>剑指 Offer 06. 从尾到头打印链表 Java 一.资源 题目: 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回). 示例 1: 输入: ...
- LeetCode Algorithm 剑指 Offer 06. 从尾到头打印链表
剑指 Offer 06. 从尾到头打印链表 Ideas 遍历链表,每次在vector的头部insert当前元素值. Code C++ class Solution {public:vector< ...
- 剑指 Offer 06. 从尾到头打印链表(python3编写)
目录 1.题目描述: 2.方法一:使用栈保存结果 思路: 代码(迭代法): 代码(递归法): 3.方法二:反转链表 思路: 代码: 4.方法三:保存数组,再反转数组 思路: 代码: 5.总结: 1.题 ...
- 剑指 Offer 06. 从尾到头打印链表(递归、逆置链表、头部动态插入)
题目 输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回). 示例 1: 输入:head = [1,3,2] 输出:[2,3,1] 限制: 0 <= 链表长度 <= 1000 ...
- Leetcode 240.剑指 Offer 06. 从尾到头打印链表 (每日一题 20210728)
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回).示例 1:输入:head = [1,3,2] 输出:[2,3,1]限制:0 <= 链表长度 <= 10000题目地址:h ...
- 剑指 Offer 06. 从尾到头打印链表
此题有两种思路,思路一:先反转链表,然后存入新建的数组中. 思路二:直接将链表存入新建的数组中,然后将数组中的数据原地置换. class Solution {public int[] reverseP ...
- 剑指 Offer 06. 从尾到头打印链表(C语言)
*输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回). 示例 1: 输入:head = [1,3,2] 输出:[2,3,1] 限制: 0 <= 链表长度 <= 10000* ...
- 【LeetCode笔记】剑指 Offer 06. 从尾到头打印链表(Java、翻转链表)
文章目录 题目描述 思路 & 代码 二刷 题目描述 思路 & 代码 一次遍历:翻转链表 + 记录长度 二次遍历:遍历翻转后的链表,更新数组 O(n) & O(1) /*** D ...
最新文章
- 有了这份程序员面试指南,你离大厂 Offer 还远吗?| 附推荐书籍
- 概要设计阶段--组装测试计划
- 移动简单网站开发遇到的问题,备忘或者分享
- STRUTS2.X之使用validate方法验证数据
- 大数据与数据挖掘考试题_2017-2019年全国Ⅱ卷高考考点数据分析(理综合)
- vue(一)vue-cli安装
- 为什么说社区团购进行得如火如荼?
- .NET Oject And Json
- 制冷与低温技术原理压焓图和制冷动画
- MySQL 8.0.28 忘记密码,重置密码
- python 归一化方法
- 【Halcon】图像预处理
- H5神兽,优优,牛来了,牛小天,牛欢喜,至尊星空等搭建教程
- Android资深开发者告诉你:简历这样写绝对吸引面试官眼球,面试电话接到手软
- Docker容器网络访问慢问题
- 分析 BAT 互联网巨头在大数据方向布局及大数据未来发展趋势
- java引入math包_java.math包应用
- 前端学习笔记____Vue2+Vue3全家桶
- 推荐一些学习嵌入式经典的书籍
- 关于Allele(等位基因)的理解