当前位置:网站首页>剑指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;
}
}

边栏推荐
- [combinatorics] permutation and combination (example of permutation and combination)
- Shutter widget: centerslice attribute
- [download attached] password acquisition tool lazagne installation and use
- Introduction to concurrent programming (I)
- OpenGL shader use
- SystemVerilog -- OOP -- copy of object
- repo Manifest Format
- 网上炒股开户安不安全?谁给回答一下
- [embedded] - Introduction to four memory areas
- 在网上炒股开户可以吗?资金安全吗?
猜你喜欢

Kubernetes three dozen probes and probe mode

PHP export word method (phpword)

LeetCode 0556. Next bigger element III - end of step 4

Self made pop-up input box, input text, and click to complete the event.

Laravel time zone timezone

Cloud Computing future - native Cloud

LeetCode 0556.下一个更大元素 III - 4步讲完

Download address and installation tutorial of vs2015

Use bloc to build a page instance of shutter

Integer int compare size
随机推荐
C language improvement article (wchar_t) character type
What is more elegant for flutter to log out and confirm again?
temp
flinksql是可以直接客户端建表读mysql或是kafka数据,但是怎么让它自动流转计算起来呢?
Simple factory and factory method mode
During FTP login, the error "530 login incorrect.login failed" is reported
OpenGL shader use
Is it safe to open an account for online stock speculation? Who can answer
Colleagues wrote a responsibility chain model, with countless bugs
Jsup crawls Baidu Encyclopedia
PHP導出word方法(一mht)
MySQL time zone solution
Introduction to concurrent programming (I)
Systemverilog-- OOP--对象的拷贝
使用BLoC 构建 Flutter的页面实例
实现验证码验证
Adult adult adult
Fundamentals of concurrent programming (III)
Dart: view the dill compiled code file
Flutter 退出登录二次确认怎么做才更优雅?