当前位置:网站首页>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;
}
};
边栏推荐
- Functions and pointers in 08 go language
- Siemens docking Leuze BPS_ 304i notes
- Foam exploded three times, why did Luo Yonghao put all his eggs in one basket to do ar?
- LeetCode94. 二叉树的中序遍历
- 苏黎世联邦理工学院 | 具有可变形注意Transformer 的基于参考的图像超分辨率(ECCV2022))
- Ten prohibitions for men and women in love
- Using dependent packages to directly implement paging and SQL statements
- 大模型哪家强?OpenBMB发布BMList给你答案!
- Marketing play is changeable, and understanding the rules is the key!
- The usage and Simulation Implementation of vector in STL
猜你喜欢
随机推荐
Hongjiu fruit passed the hearing: five month operating profit of 900million Ali and China agricultural reclamation are shareholders
SuperMap arsurvey license module division
03 pyechars 直角坐标系图表(示例代码+效果图)
设计一个线程池
合并表格行---三层for循环遍历数据
Unity加载Glb模型
OpenAtom OpenHarmony分论坛圆满举办,生态与产业发展迈向新征程
Leetcode: array
Holes in [apue] files
Insufficient permission to pull server code through Jenkins and other precautions
连通块&&食物链——(并查集小结)
一台电脑上 多个项目公用一个 公私钥对拉取gerrit服务器代码
Functions and pointers in 08 go language
牛客网二叉树题解
30 years of open source community | 2022 open atom global open source summit 30 years of special activities of open source community were successfully held
输入字符串,内有数字和非字符数组,例如A123x456将其中连续的数字作为一个整数,依次存放到一个数组中,如123放到a[0],456放到a[1],并输出a这些数
Aopmai biological has passed the registration: the half year revenue is 147million, and Guoshou Chengda and Dachen are shareholders
LeetCode94. 二叉树的中序遍历
Hc-05 Bluetooth module debugging slave mode and master mode experience
Foam exploded three times, why did Luo Yonghao put all his eggs in one basket to do ar?









