当前位置:网站首页>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;
}
};
边栏推荐
- 20220728 common methods of object class
- STM32F103 几个特殊引脚做普通io使用注意事项以及备份寄存器丢失数据问题1,2
- Solution to the binary tree problem of niuke.com
- Insufficient permission to pull server code through Jenkins and other precautions
- Uncover why devaxpress WinForms, an interface control, discards the popular maskbox property
- Communication example between upper computer and Mitsubishi fn2x
- 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
- 金山云冲刺港股拟双重主要上市:年营收90亿 为雷军力挺项目
- Leetcode: array
- 开源汇智创未来 | 2022 开放原子全球开源峰会 OpenAtom openEuler 分论坛圆满召开
猜你喜欢

HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing

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

线性分类器(CCF20200901)

How to realize more multimedia functions through the ffmpeg library and NaPi mechanism integrated in openharmony system?

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

行业落地呈现新进展 | 2022 开放原子全球开源峰会 OpenAtom OpenHarmony 分论坛圆满召开

【一知半解】零值拷贝

Not optimistic about Apple making AR, Luo Yonghao: I'll do it myself

一台电脑上 多个项目公用一个 公私钥对拉取gerrit服务器代码

Uncover why devaxpress WinForms, an interface control, discards the popular maskbox property
随机推荐
云原生—运行时环境
STM32F103 several special pins are used as ordinary io. Precautions and data loss of backup register 1,2
[apue] 文件中的空洞
OpenAtom OpenHarmony分论坛圆满举办,生态与产业发展迈向新征程
Unity loads GLB model
Multi Chain and multi currency wallet system development cross chain technology
Uniapp 应用开机自启插件 Ba-Autoboot
New Oriental's single quarter revenue was 524million US dollars, a year-on-year decrease of 56.8%, and 925 learning centers were reduced
AVL tree (balanced search tree)
How to realize more multimedia functions through the ffmpeg library and NaPi mechanism integrated in openharmony system?
合并表格行---三层for循环遍历数据
卸载 Navicat:正版 MySQL 官方客户端,真香!
How does musk lay off staff?
开源汇智创未来 | 2022 开放原子全球开源峰会 OpenAtom openEuler 分论坛圆满召开
SuperMap itablet license module division
20220728-Object类常用方法
Yan Ji lost Beijing again, and more than half of the stores in the country were closed
Unity 安装 Device Simulator
C for循环内定义int i变量出现的重定义问题
Merge table rows - three levels of for loop traversal data