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

边栏推荐
- AOSP ~ NTP (Network Time Protocol)
- Is it safe to open an account for online stock speculation? Who can answer
- 102. Sequence traversal of binary tree
- OpenGL index cache object EBO and lineweight mode
- Swagger
- [download attached] password acquisition tool lazagne installation and use
- Why can't my MySQL container start
- pragma-pack语法与使用
- Basic knowledge of OpenGL (sort it out according to your own understanding)
- temp
猜你喜欢

PHP導出word方法(一mht)

Kubernetes three dozen probes and probe mode

If you can't learn, you have to learn. Jetpack compose writes an im app (II)
![[MySQL special] read lock and write lock](/img/ac/e01c26882cc664ea2e5e731c5a8bab.png)
[MySQL special] read lock and write lock

Itext7 uses iexternalsignature container for signature and signature verification

Swagger

Wechat applet - basic content

(构造笔记)ADT与OOP

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

为什么我的mysql容器启动不了呢
随机推荐
Use of atomicinteger
JVM内存模型
Self made pop-up input box, input text, and click to complete the event.
error: expected reference but got (raw string)
2020-10_ Development experience set
New features of ES6
Socket TCP for network communication (I)
DEJA_VU3D - Cesium功能集 之 053-地下模式效果
Adult adult adult
225. Implement stack with queue
【mysql专项】读锁和写锁
抓包整理外篇fiddler———— 会话栏与过滤器[二]
Computer version wechat applet full screen display method, mobile phone horizontal screen method.
云计算未来 — 云原生
(構造筆記)從類、API、框架三個層面學習如何設計可複用軟件實體的具體技術
PHP导出word方法(一phpword)
What is more elegant for flutter to log out and confirm again?
PHP export word method (phpword)
4000字超详解指针
Swagger