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

边栏推荐
- Wechat applet development - page Jump transfer parameters
- 2020-10_ Development experience set
- Basic knowledge of OpenGL (sort it out according to your own understanding)
- 在网上炒股开户可以吗?资金安全吗?
- 使用BLoC 构建 Flutter的页面实例
- 145. Post order traversal of binary tree
- Use of QT OpenGL camera
- Is it OK to open an account for online stock speculation? Is the fund safe?
- Lambda expression
- Adult adult adult
猜你喜欢

Take you to the installation and simple use tutorial of the deveco studio compiler of harmonyos to create and run Hello world?

实现验证码验证

(construction notes) ADT and OOP

QT OpenGL texture map

Display time with message interval of more than 1 minute in wechat applet discussion area

If you can't learn, you have to learn. Jetpack compose writes an im app (II)

Symlink(): solution to protocol error in PHP artisan storage:link on win10

The future of cloud computing cloud native

Qt+vtk+occt reading iges/step model

ES6 standard
随机推荐
Redis notes 01: Introduction
Pragma pack syntax and usage
(database authorization - redis) summary of unauthorized access vulnerabilities in redis
repo Manifest Format
PHP導出word方法(一mht)
Capturing and sorting out external Fiddler -- Conversation bar and filter [2]
Dart: view the dill compiled code file
1-1 token
Symlink(): solution to protocol error in PHP artisan storage:link on win10
Socket TCP for network communication (I)
剑指Offer10- I. 斐波那契数列
Atomic atomic operation
023 ([template] minimum spanning tree) (minimum spanning tree)
347. Top k high frequency elements
Itext7 uses iexternalsignature container for signature and signature verification
Flutter Widget : KeyedSubtree
【附下载】密码获取工具LaZagne安装及使用
[official MySQL document] deadlock
Kubectl_ Command experience set
(构造笔记)MIT reading部分学习心得