当前位置:网站首页>LeetCode206 反转链表
LeetCode206 反转链表
2022-07-28 11:49:00 【.DoubleBean.】
LeetCode206. 反转链表
https://leetcode-cn.com/problems/reverse-linked-list/
题目描述
反转一个单链表。
示例:
输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
进阶:
你可以迭代或递归地反转链表。你能否用两种方法解决这道题?
思路
①双指针迭代
申请两个指针pre(最初指向NULL),cur(最初指向head),然后cur不断向后遍历,每次先保存下cur的下一个结点,之后将cur的next指针指向pre,然后pre和cur前进一位
代码
/** * 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) {
//申请结点,pre和cur,pre指向NULL,cur指向head
ListNode* pre = NULL;
ListNode* cur = head;
while(cur){
//保存cur的下一个结点
ListNode* temp = cur->next;
//将cur的next指针指向pre,进行翻转操作
cur->next = pre;
//更新pre和cur
pre = cur;
cur = temp;
}
return pre;
}
};
复杂度分析:
令 n 为数组长度。
- 时间复杂度: O ( n ) O(n) O(n) 需要遍历链表一次。
- 空间复杂度: O ( 1 ) O(1) O(1)
②递归法
按照双指针法思路来写的递归
代码
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);
}
};
还有一种递归,稍微难理解一些
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head==NULL || head->next == NULL)
return head;
//这里的cur就是最后一个结点
ListNode* cur = reverseList(head->next);
//head的下一个结点的next指针指向head
head->next->next = head;
//防止链表循环,使head的next指针为空
head->next = NULL;
//每层递归都返回cur,也就是最后一个结点
return cur;
}
};
边栏推荐
- VS1003调试例程
- [nuxt 3] (XII) project directory structure 3
- IO流再回顾,深入理解序列化和反序列化
- Design a thread pool
- 用C语言开发NES游戏(CC65)05、调色板
- HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing
- C# 泛型是什么、泛型缓存、泛型约束
- Using dependent packages to directly implement paging and SQL statements
- Kuzaobao: summary of Web3 encryption industry news on July 13
- Basic use of JSON server
猜你喜欢

软件架构师必需要了解的 saas 架构设计?

Foam exploded three times, why did Luo Yonghao put all his eggs in one basket to do ar?

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

GMT安装与使用

VS code更新后不在原来位置

图书馆自动预约脚本

leetcode:704二分查找

Developing NES games with C language (cc65) 11. Metatiles
![[half understood] zero value copy](/img/5b/18082c1ea93f2e3bbf4920d73163fd.png)
[half understood] zero value copy

行业落地呈现新进展 | 2022 开放原子全球开源峰会 OpenAtom OpenHarmony 分论坛圆满召开
随机推荐
合并表格行---三层for循环遍历数据
Developing NES games with C language (cc65) 02. What is v-blank?
VS1003调试例程
用C语言开发NES游戏(CC65)06、精灵
Some API interfaces purchased by Yiwu hope to bring you some help
The usage and Simulation Implementation of vector in STL
Using Arduino to develop esp8266 to build a development environment
力扣315计算右侧小于当前元素的个数
sqli-labs(less-8)
Library automatic reservation script
上位机和三菱FN2x通信实例
Marketing play is changeable, and understanding the rules is the key!
With the continuous waves of infringement, the U.S. patent and trademark office began to study the impact of NFT on copyright
Functions and pointers in 08 go language
【萌新解题】爬楼梯
Tik tok "founder" Yang Luyu, farewell byte?
STM32 loopback structure receives and processes serial port data
[cute new problem solving] climb stairs
Aopmai biological has passed the registration: the half year revenue is 147million, and Guoshou Chengda and Dachen are shareholders
Minimally invasive electrophysiology has passed the registration: a listed enterprise with annual revenue of 190million minimally invasive mass production