当前位置:网站首页>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;
}
}
边栏推荐
- [video game training] non contact object size and shape measurement 2020 video game G
- Blockbuster | certik: Web3.0 industry safety report release in the second quarter of 2022 (PDF download link attached)
- Table custom table encapsulation
- [QNX Hypervisor 2.2用户手册]9.1 配置变量
- Tutorial on the principle and application of database system (048) -- MySQL query (x): self connection query
- scroll-view实现下拉刷新(避免onload进入页面初始refresher-triggered为true触发下拉问题)
- [flyway introduction]
- The high-quality digital collection of guochuang's "children's song line" is on sale, and you are invited to create a young martial arts Jianghu dream
- What impact does the European "gas shortage" have on China?
- 采坑websocket總結
猜你喜欢

Introduction to QT (2.1 the first procedure for the beginning of QT)

落枕如何快速缓解

Creo 9.0 mouse button operation for model observation

Classic examples of C language - use 4 × The matrix displays all integers from 1 to 16 and calculates the sum of each row, column, and diagonal

如何在自动化测试中使用MitmProxy获取数据返回?

GLIB-CRITICAL g_ file_ test:assertion ‘filename != null‘ failed

黑马程序员-接口测试-四天学习接口测试-第四天-Postman读取外部数据文件,读取数据文件数据,iHRM项目实战,员工管理模块,添加员工,批量运行测试用例,生成测试报告,

How can dbcontext support the migration of different databases in efcore advanced SaaS system

Sparksql design and introduction, 220722,

Create a self signed certificate to digitally sign exe files
随机推荐
IDEA 热部署(热加载)
Classic example of C language - loan balance
Treatment of particle boundary collision
[QNX Hypervisor 2.2用户手册]9.1 配置变量
Leetcode set the intersection size to at least 2
Introduction to several scenarios involving programming operation of Excel in SAP implementation project
Prometheus+node exporter+grafana monitoring server system resources
C language book recommendation
Tutorial on principles and applications of database system (041) -- MySQL query (III): setting query conditions
《天幕红尘》笔记与思考(六)因缺而需
scroll-view實現下拉刷新(避免onload進入頁面初始refresher-triggered為true觸發下拉問題)
[data mining engineer - written examination] Haier company in 2022
This is a big problem
js的相关知识
How to use SAP intelligent robotic process automation to automate Excel
这是一道大水题
Bean Validation使用篇----05
mysql 分支语句case报错
[the 83rd fortnight of leetcode]
SAP 电商云 Spartacus UI Store 相关的设计明细