当前位置:网站首页>Sword finger offer06 Print linked list from end to end
Sword finger offer06 Print linked list from end to end
2022-07-03 12:27:00 【Wu Liuqi】
The finger of the sword Offer06. Print linked list from end to end
Title Description
Enter the head node of a linked list , Return the value of each node from the end to the end ( Return with array ).
Example 1:
Input :head = [1,3,2]
Output :[2,3,1]
Limit :
0 <= Chain length <= 10000
JAVA Code ( Complete four methods )
It's easy to do a problem , But how to solve the problem in many ways , Think from multiple perspectives , Can we exercise our logical thinking ability more .
Reverse insertion
Get the length of the linked list and create an array , Traverse the linked list from the beginning , Insert array in reverse order .
/** * 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;
}
// Get the length of the list
public int getLength(ListNode head){
ListNode p = head;
int n = 0;
while(p!=null){
p = p.next;
n++;
}
return n;
}
}
Stack method
Stack linked list , Then put it out of the stack into the array .
/** * 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;
}
}
Reverse linked list method
/** * Definition for singly-linked list. * public class ListNode { * int val; * ListNode next; * ListNode(int x) { val = x; } * } */
class Solution {
public int[] reversePrint(ListNode head) {
// Reverse the linked list
ListNode p = head;
if(p==null){
return new int[0];
}
ListNode q = p.next;
int len = 1;
while(q!=null){
// Save the node of the next operation
ListNode next = q.next;
q.next = p;
p = q;
q = next;
len++;
}
// Handle tail nodes
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 Law
Save the linked list data to ArrayList in , Then put it in the array in reverse order
/** * 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;
}
}
边栏推荐
- DEJA_ Vu3d - 054 of cesium feature set - simulate the whole process of rocket launch
- Dart: about Libraries
- SystemVerilog -- OOP -- copy of object
- Official website of Unicode query
- 20. Valid brackets
- 023(【模板】最小生成树)(最小生成树)
- 232. Implement queue with stack
- PHP導出word方法(一mht)
- Oracle advanced (I) realize DMP by expdp impdp command
- 4000 word super detailed pointer
猜你喜欢
随机推荐
temp
What is more elegant for flutter to log out and confirm again?
剑指Offer10- I. 斐波那契数列
Redis notes 01: Introduction
The difference between lambda and anonymous inner class
If you can't learn, you have to learn. Jetpack compose writes an im app (II)
elastic_ L04_ introduction. md
239. Sliding window maximum
How to deploy web pages to Alibaba cloud
(构造笔记)从类、API、框架三个层面学习如何设计可复用软件实体的具体技术
Redis
剑指Offer09. 用两个栈实现队列
【附下载】密码获取工具LaZagne安装及使用
PHP export word method (phpword)
(构造笔记)GRASP学习心得
wpa_ cli
The future of cloud computing cloud native
ES6新特性
手机号码变成空号导致亚马逊账号登陆两步验证失败的恢复网址及方法
Integer int compare size