当前位置:网站首页>[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 Qingyuan fixed assets management and barcode inventory system
- 2022.6.6-----leetcode. seven hundred and thirty-two
- Classic questions: 01 backpack, complete backpack, multiple backpack, two-dimensional cost Backpack
- Set up a flag -- Reconstruct promise
- [ROS] review of 2021 ROS Summer School
- Shenzhen Nanshan District specialized special new enterprise application process, with a subsidy of RMB 100000-500000
- Sealem Finance打造Web3去中心化金融平台基础设施
- 2022.6.6-----leetcode.732
- Leetcode 698 partition to K equal sum subsets (DFS pruning)
- threejs:如何获得几何体的boundingBox?
猜你喜欢

detectron2训练自己的数据集和转coco格式

Threejs: pit encountered in drawing Bessel curve with two-point coordinates

2021-2-14 gephi学习笔记

Project_ Visual analysis of epidemic data based on Web Crawler

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

Project_ Visual analysis of epidemic data based on Web Crawler
![[Li mu] how to read papers [intensive reading of papers]](/img/41/7e1ff1db2f7a848c8702c186c79fe5.jpg)
[Li mu] how to read papers [intensive reading of papers]

Yunna PDA wireless fixed assets inventory management system

Threejs: streamer effect encapsulation

1.5、PX4载具选择
随机推荐
Project_ Visual analysis of epidemic data based on Web Crawler
2.0、ROS与PX4通信详解
神经网络极简史,神经网络知识点整理
Yanrong looks at how to realize the optimal storage solution of data Lake in a hybrid cloud environment
Using completabilefuture
Web3 ecological decentralized financial platform sealem Finance
Is the SQL query result different from what you expected? Mostly "null" is making trouble
2.0 detailed explanation of ROS and Px4 communication
How about compound interest insurance and financial products? Can I buy it?
焱融看|混合云环境下,如何实现数据湖最优存储解决方案
Hao expresses his opinions: what small good habits have you adhered to?
Understanding of multithreading
PX4装机教程(六)垂起固定翼(倾转)
Inventory management and strategy mode
"It looks like robbing tickets but actually robbing money". Don't be fooled by fancy ticket robbing products again and again
Threejs: pit encountered in drawing Bessel curve with two-point coordinates
1.6 Px4 initialization calibration
Sealem Finance打造Web3去中心化金融平台基础设施
Multipartfile and file interoperation tool classes
“看似抢票实际抢钱”,别被花式抢票产品一再忽悠