当前位置:网站首页>[leetcode] reverse linked list
[leetcode] reverse linked list
2022-06-11 01:44:00 【xiaoshijiu333】
#LeetCode A daily topic 【 Special topic of linked list 】
Reverse a linked list
https://leetcode-cn.com/problems/reverse-linked-list/analysis
Here's the head node of the list head , Please reverse the list , And return the inverted linked list .
1——>2——>3——>4——>5
become
5——>4——>3——>2——>1Realization
Non recursive : Node by node processing , Record the location of the previous node and the location of the next node , Set the... Of the current node next Point to the previous node
public ListNode reverseList(ListNode head) {
// Reverse a linked list
ListNode prev = null, next;
while (head != null) {
next = head.next;
head.next = prev;
prev = head;
head = next;
}
return prev;
}
LeetCode Time consuming :0ms
recursive : The principle is the same , Set the... Of the current node next Point to the previous node
public ListNode reverseList(ListNode head) {
return reverse(head,null);
}
/* Use recursion to reverse the linked list */
public ListNode reverse(ListNode head, ListNode prev) {
if (head == null) {
return prev;
}
ListNode next = head.next;
head.next = prev;
return reverse(next, head);
}
LeetCode Time consuming :0ms
- summary :
Traversal is good at solving problems from the front to the next , Sometimes I want to use recursion to realize traversal , It is often necessary to add a parameter to record the results of operations from front to back ;
However, recursion is often used in , Recursion to the deepest point requires some operations , And then add up , I.e. upward tracing .
边栏推荐
- Yunna PDA wireless fixed assets inventory management system
- Hooks' design philosophy
- 2021-2-26编程语言知识点整理
- 1.7、PX4遥控器校准
- SAS判别分析(Bayes准则和proc discrim过程)
- QGC地面站使用教程
- 1.6、 PX4初始化校准
- Sealem Finance打造Web3去中心化金融平台基础设施
- 1.2. Ros+px4 preliminary basic knowledge
- Shenzhen Nanshan District specialized special new enterprise application process, with a subsidy of RMB 100000-500000
猜你喜欢

项目_基于网络爬虫的疫情数据可视化分析
![[ROS introduction] cmakelist Txt and packages XML interpretation](/img/26/bae82a457fb83b2214d2f8c20955e2.jpg)
[ROS introduction] cmakelist Txt and packages XML interpretation

Inventory management and strategy mode

How to write this with data and proc without SQL

There is a problem with numpy after CONDA installs pytoch

Is the SQL query result different from what you expected? Mostly "null" is making trouble

SAS因子分析(proc factor过程和因子旋转以及回归法求因子得分函数)

From "0" to "tens of millions" concurrency, 14 technological innovations of Alibaba distributed architecture
![[geometric vision] 4.2 piecewise linear transformation](/img/9e/ad010e0b55c88f2c0244442ae20fb3.jpg)
[geometric vision] 4.2 piecewise linear transformation

What are programmers in big factories looking at? It took me two years to sort it out, and I will look at it and cherish it!
随机推荐
函数的节流和防抖
Bad RequestThis combination of host and port requires TLS.
[VBA Script] extract the information and pending status of all annotations in the word document
1.4PX4程序下载
Is the SQL query result different from what you expected? Mostly "null" is making trouble
PX4从放弃到精通(二十四):自定义机型
Yunna provincial administrative unit fixed assets management system
How to write this with data and proc without SQL
2.1、ROS+PX4仿真---定点飞行控制
Uninstall mavros
Project_ Visual analysis of epidemic data based on Web Crawler
Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack
MultipartFile和File互转工具类
Some tips for programmers to deal with stress
1.6 Px4 initialization calibration
Leetcode string problem
Tencent Cloud Database tdsql - Dajia comments | The Past, Present and Future of basic software
1.5、PX4载具选择
Leetcode 1814 count nice pairs in an array (recommended by map)
[ongoing update...] 2021 National Electronic Design Competition for college students (III) interpretation of the anonymous four axis space developer flight control system design