当前位置:网站首页>Sword *offer -- reverse order of linked list
Sword *offer -- reverse order of linked list
2022-07-24 00:55:00 【wzf6667】
Reverse order of linked list
Enter a linked list , Return a list from the end to the end ArrayList.
There are many ways , Because I am not familiar with the linked list, I focused on the reverse order of the linked list , Other relatively simple methods are Stack( Why didn't I expect !), Recursion and placement ArrayList Then recycle in reverse order .
Reverse order of linked list
Highlight !** First, let p==null; In order to connect the tail of the linked list null
Be careful temp = q.next The location of , Be sure to put it in the first sentence .
/** * public class ListNode { * int val; * ListNode next = null; * * ListNode(int val) { * this.val = val; * } * } * */
import java.util.ArrayList;
public class Solution {
public ArrayList<Integer> printListFromTailToHead(ListNode listNode) {
ArrayList<Integer> array = new ArrayList<>();
if(listNode == null ){
return array;
}
if(listNode.next==null){
array.add(listNode.val);
return array;
}
ListNode p = null;
ListNode q = listNode;
ListNode temp = q.next;
while(q != null){
temp = q.next;
q.next = p;
p = q;
q = temp;
}
while(p!=null){
array.add(p.val);
p=p.next;
}
return array;
}
}
边栏推荐
- SAP 电商云 Spartacus UI Store 相关的设计明细
- There are various signs that apple is expected to support AV1
- The postman test interface has 404 or 500 errors when the URL is configured correctly
- JS drag and drop element
- This is a big problem
- Classic example of C language - loan balance
- ES6 combines multiple class methods
- C language book recommendation
- AVX instruction set accelerated matrix multiplication
- EFCore高级Saas系统下单DbContext如何支持不同数据库的迁移
猜你喜欢

Summary of polynomial commitment schemes

C language: deep analysis of const keyword

Database connection pool & dbutils

落枕如何快速缓解

網絡系統實驗:ping不通的問題解决

MariaDB database upgrade version

PostgreSQL snapshot optimization globalvis new system analysis (greatly enhanced performance)

Image processing 1:rgb888_ YCbCr444

Redis | very important Middleware

QT入门篇(2.1初入QT的开始第一个程序)
随机推荐
Error running ‘XXX‘: Command line is too long. Shorten command line for AudioTest or also ...
项目场景:nvidia-smi Unable to datemine the device handle for GPU 0000:01:00.0: Unknow Error
Tutorial on principles and applications of database system (052) -- data integrity of MySQL (XIV): crosstab query (row column conversion)
[flyway introduction]
Tutorial on principles and applications of database system (039) -- MySQL query (I): syntax analysis of select command
Programmeur de cheval noir - test d'interface - test d'interface d'apprentissage de quatre jours - jour 4 - Postman lit des fichiers de données externes, lit des données de fichiers de données, IHRM P
MySQL exercise: all employees reporting to the CEO
Bean Validation使用篇----05
Pbootcms database conversion tutorial (SQLite to MySQL detailed tutorial)
多源文件方式去访问全局变量的方式(extern用法)
Create a self signed certificate to digitally sign exe files
C language book recommendation
scroll-view实现下拉刷新(避免onload进入页面初始refresher-triggered为true触发下拉问题)
暑假第四周总结
Development of main applet for business card traffic near the map
Case error of MySQL branch statement
这是一道大水题
vim常用命令
There are various signs that apple is expected to support AV1
Bean Validation自定义容器验证篇----06