当前位置:网站首页>剑指Offer06. 从尾到头打印链表
剑指Offer06. 从尾到头打印链表
2022-07-03 11:50:00 【伍六琪】
剑指Offer06. 从尾到头打印链表
题目描述
输入一个链表的头节点,从尾到头反过来返回每个节点的值(用数组返回)。
示例 1:
输入:head = [1,3,2]
输出:[2,3,1]
限制:
0 <= 链表长度 <= 10000
JAVA代码(完整的四种方法)
做一道题很容易,但是如何多种方式解决问题,多种角度思考问题,才能更加锻炼我们的逻辑思维能力。
倒序插入
获取链表长度创建数组,将链表从头遍历,倒序插入数组。
/** * 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 p = head;
int len = getLength(head);
int[] arr = new int[len];
int index = len-1;
while(index>=0){
arr[index] = p.val;
p = p.next;
index--;
}
return arr;
}
//获取链表长度
public int getLength(ListNode head){
ListNode p = head;
int n = 0;
while(p!=null){
p = p.next;
n++;
}
return n;
}
}

栈方法
将链表入栈,再出栈放入数组中。
/** * 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<Integer> stack = new Stack<Integer>();
ListNode p = head;
while(p!=null){
stack.push(p.val);
p = p.next;
}
int size = stack.size();
int[] arr = new int[size];
for(int i = 0;i<size;i++){
arr[i] = stack.pop();
}
return arr;
}
}

逆转链表法
/** * 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 p = head;
if(p==null){
return new int[0];
}
ListNode q = p.next;
int len = 1;
while(q!=null){
//保存下一个操作的结点
ListNode next = q.next;
q.next = p;
p = q;
q = next;
len++;
}
//处理尾结点
head.next = null;
int[] arr = new int[len];
ListNode l = p;
int index = 0;
while(l!=null){
arr[index++] = l.val;
l = l.next;
}
return arr;
}
}

ArrayList法
将链表数据存到ArrayList中,再逆序放到数组中
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution {
public int[] reversePrint(ListNode head) {
ArrayList<Integer> list = new ArrayList<Integer>();
ListNode p = head;
int n = 0;
while(p!=null){
list.add(p.val);
p = p.next;
n++;
}
int[] arr = new int[n];
for(int i = 0;i<n;i++){
arr[i] = list.get(n-i-1);
}
return arr;
}
}

边栏推荐
- temp
- LeetCode 0556.下一个更大元素 III - 4步讲完
- PHP get the file list and folder list under the folder
- 023 ([template] minimum spanning tree) (minimum spanning tree)
- OpenGL draws colored triangles
- [MySQL special] read lock and write lock
- (construction notes) learn the specific technology of how to design reusable software entities from three levels: class, API and framework
- [embedded] - Introduction to four memory areas
- Swagger
- 102. Sequence traversal of binary tree
猜你喜欢

PHP export word method (one MHT)

Develop plug-ins for idea

4000字超详解指针

Integer string int mutual conversion

PHP导出word方法(一mht)

Shutter: overview of shutter architecture (excerpt)

Integer int compare size

2.8 overview of ViewModel knowledge

云计算未来 — 云原生

Is BigDecimal safe to calculate the amount? Look at these five pits~~
随机推荐
4000字超详解指针
网上炒股开户安不安全?谁给回答一下
Summary of development issues
How to convert a numeric string to an integer
OpenGL index cache object EBO and lineweight mode
What is more elegant for flutter to log out and confirm again?
【附下载】密码获取工具LaZagne安装及使用
Develop plug-ins for idea
PHP導出word方法(一mht)
使用BLoC 构建 Flutter的页面实例
Shell: basic learning
Differences between MySQL Union and union all
Wechat applet pages always report errors when sending values to the background. It turned out to be this pit!
在网上炒股开户可以吗?资金安全吗?
【嵌入式】---- 内存四区介绍
4000 word super detailed pointer
New features of ES6
Solve msvcp120d DLL and msvcr120d DLL missing
typeScript
Computer version wechat applet full screen display method, mobile phone horizontal screen method.