当前位置:网站首页>Leetcode206 reverse linked list
Leetcode206 reverse linked list
2022-07-28 12:46:00 【.DoubleBean.】
LeetCode206. Reverse a linked list
https://leetcode-cn.com/problems/reverse-linked-list/
Title Description
Invert a single chain table .
Example :
Input : 1->2->3->4->5->NULL
Output : 5->4->3->2->1->NULL
Advanced :
You can reverse the list iteratively or recursively . Can you solve the problem in two ways ?
Ideas
① Double pointer iteration
Apply for two pointers pre( At first it pointed to NULL),cur( At first it pointed to head), then cur Keep going backwards , Save it first every time cur Next node of , After the cur Of next Pointer to pre, then pre and cur Advance one 
Code
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode() : val(0), next(nullptr) {} * ListNode(int x) : val(x), next(nullptr) {} * ListNode(int x, ListNode *next) : val(x), next(next) {} * }; */
class Solution {
public:
ListNode* reverseList(ListNode* head) {
// Application node ,pre and cur,pre Point to NULL,cur Point to head
ListNode* pre = NULL;
ListNode* cur = head;
while(cur){
// preservation cur Next node of
ListNode* temp = cur->next;
// take cur Of next Pointer to pre, Turn it over
cur->next = pre;
// to update pre and cur
pre = cur;
cur = temp;
}
return pre;
}
};
Complexity analysis :
Make n Is array length .
- Time complexity : O ( n ) O(n) O(n) You need to traverse the linked list once .
- Spatial complexity : O ( 1 ) O(1) O(1)
② Recursive method
Recursion written according to the idea of double pointer method
Code
class Solution {
public:
ListNode* reverse(ListNode* pre, ListNode* cur){
if(!cur)
return pre;
ListNode* temp = cur->next;
cur->next = pre;
return reverse(cur, temp);
}
ListNode* reverseList(ListNode* head) {
return reverse(NULL, head);
}
};
There is also a recursion , It's a little difficult to understand 
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL || head->next == NULL)
return head;
// there cur Is the last node
ListNode* cur = reverseList(head->next);
//head The next node of next Pointer to head
head->next->next = head;
// Prevent the list from cycling , send head Of next The pointer is null
head->next = NULL;
// Each level of recursion returns cur, That's the last node
return cur;
}
};
边栏推荐
- New progress in the implementation of the industry | the openatom openharmony sub forum of the 2022 open atom global open source summit was successfully held
- 20220728-Object类常用方法
- 洪九果品通过聆讯:5个月经营利润9亿 阿里与中国农垦是股东
- 【一知半解】零值拷贝
- STM32F103 几个特殊引脚做普通io使用注意事项以及备份寄存器丢失数据问题1,2
- 试用copilot过程中问题解决
- Leetcode:704 binary search
- Not optimistic about Apple making AR, Luo Yonghao: I'll do it myself
- scala 转换、过滤、分组、排序
- 图书馆自动预约脚本
猜你喜欢

scala 转换、过滤、分组、排序

Leetcode:704 binary search

Baidu map API adds information window circularly. The window only opens at the last window position and the window information content is the same solution

Redis实现分布式锁

卸载 Navicat:正版 MySQL 官方客户端,真香!

LeetCode94. 二叉树的中序遍历

IO流再回顾,深入理解序列化和反序列化

GMT安装与使用

新零售电商O2O模式解析

Sub database and sub table may not be suitable for your system. Let's talk about how to choose sub database and sub table and newsql
随机推荐
[nuxt 3] (XII) project directory structure 3
新零售电商O2O模式解析
STM32F103 几个特殊引脚做普通io使用注意事项以及备份寄存器丢失数据问题1,2
公司在什么情况下可以开除员工
01 pyechars 特性、版本、安装介绍
单调栈Monotonic Stack
Brief discussion on open source OS distribution
The openatom openharmony sub forum was successfully held, and ecological and industrial development entered a new journey
奥浦迈生物通过注册:半年营收1.47亿 国寿成达与达晨是股东
1331. Array sequence number conversion: simple simulation question
Unity加载Glb模型
OpenAtom OpenHarmony分论坛圆满举办,生态与产业发展迈向新征程
04 pyechars 地理图表(示例代码+效果图)
软件架构师必需要了解的 saas 架构设计?
LeetCode94. 二叉树的中序遍历
Uniapp 应用开机自启插件 Ba-Autoboot
The 'name' attribute value associated with the element type 'item' cannot contain '& lt;' Character solution
遭受痛苦和创伤后的四种本真姿态 齐泽克
Unity installs the device simulator
leetcode 1518. 换酒问题