当前位置:网站首页>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;
}
};
边栏推荐
- Localization, low latency, green and low carbon: Alibaba cloud officially launched Fuzhou data center
- 十三. 实战——常用依赖的作用
- STM32F103 several special pins are used as ordinary io. Precautions and data loss of backup register 1,2
- Newly released, the domestic ide developed by Alibaba is completely open source
- 牛客网二叉树题解
- Markdown concise grammar manual
- SuperMap arsurvey license module division
- 非标自动化设备企业如何借助ERP系统,做好产品质量管理?
- Developing NES games with C language (cc65) 09, scrolling
- Minimally invasive electrophysiology has passed the registration: a listed enterprise with annual revenue of 190million minimally invasive mass production
猜你喜欢

开源社区三十年 | 2022 开放原子全球开源峰会开源社区三十年专题活动圆满召开

AsiaInfo technology released antdb7.0, a "Telecom grade" core transaction database, to help government and enterprises "trust" to create the future!

GMT安装与使用

Anhui Jingzhun: Beidou satellite synchronous clock | Beidou synchronous clock | NTP network clock server

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

Unity 安装 Device Simulator

Developing NES games with C language (cc65) 08. Background collision

VS code更新后不在原来位置

VS1003调试例程

05 pyechars 基本图表(示例代码+效果图)
随机推荐
leetcode:数组
[dark horse morning post] LETV 400 employees have no 996 and no internal papers; Witness history! 1 euro =1 US dollar; Stop immediately when these two interfaces appear on wechat; The crackdown on cou
Custom paging tag 02 of JSP custom tag
AI制药的数据之困,分子建模能解吗?
The 'name' attribute value associated with the element type 'item' cannot contain '& lt;' Character solution
Multi Chain and multi currency wallet system development cross chain technology
HMS core audio editing service supports 7 kinds of audio effects to help one-stop audio processing
20220728-Object类常用方法
连通块&&食物链——(并查集小结)
用C语言开发NES游戏(CC65)03、VRAM缓冲区
05 pyechars 基本图表(示例代码+效果图)
用C语言开发NES游戏(CC65)02、什么是v-blank?
Siemens docking Leuze BPS_ 304i notes
上位机和三菱FN2x通信实例
Developing NES games with C language (cc65) 02. What is v-blank?
设计一个线程池
Introduction to resttemplate
Some API interfaces purchased by Yiwu hope to bring you some help
[Nuxt 3] (十二) 项目目录结构 3
Anhui Jingzhun: Beidou satellite synchronous clock | Beidou synchronous clock | NTP network clock server