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

边栏推荐
- PHP export word method (phpword)
- Cloud Computing future - native Cloud
- 网络通讯之Socket-Tcp(一)
- repo Manifest Format
- Display time with message interval of more than 1 minute in wechat applet discussion area
- Talk about the state management mechanism in Flink framework
- [MySQL special] read lock and write lock
- OpenGL draws colored triangles
- Is it OK to open an account for online stock speculation? Is the fund safe?
- DEJA_ Vu3d - cesium feature set 053 underground mode effect
猜你喜欢

Pki/ca and digital certificate

Integer string int mutual conversion

OpenGL index cache object EBO and lineweight mode

剑指Offer10- I. 斐波那契数列

【mysql专项】读锁和写锁

Use bloc to build a page instance of shutter

2.8 overview of ViewModel knowledge

New features of ES6

【附下载】密码获取工具LaZagne安装及使用

Itext7 uses iexternalsignature container for signature and signature verification
随机推荐
2020-10_ Development experience set
LeetCode 0556.下一个更大元素 III - 4步讲完
【附下载】密码获取工具LaZagne安装及使用
023 ([template] minimum spanning tree) (minimum spanning tree)
Adult adult adult
[download attached] password acquisition tool lazagne installation and use
Integer string int mutual conversion
Shardingsphere sub database and sub table < 3 >
Use of atomicinteger
2.8 overview of ViewModel knowledge
New features of ES6
Is it OK to open an account for online stock speculation? Is the fund safe?
(构造笔记)ADT与OOP
Lambda表达式
Redis notes 01: Introduction
Implement verification code verification
Apprendre à concevoir des entités logicielles réutilisables à partir de la classe, de l'API et du cadre
Pki/ca and digital certificate
error: expected reference but got (raw string)
Itext7 uses iexternalsignature container for signature and signature verification