当前位置:网站首页>【LeetCode】206.反转链表
【LeetCode】206.反转链表
2022-08-02 02:40:00 【酥酥~】
题目
给你单链表的头节点 head ,请你反转链表,并返回反转后的链表。
示例 1:
输入:head = [1,2,3,4,5]
输出:[5,4,3,2,1]
示例 2:
输入:head = [1,2]
输出:[2,1]
示例 3:
输入:head = []
输出:[]
提示:
链表中节点的数目范围是 [0, 5000]
-5000 <= Node.val <= 5000
进阶:链表可以选用迭代或递归方式完成反转。你能否用两种方法解决这道题?
题解
使用迭代方法
/** * 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) {
ListNode* result = new ListNode(0);
while(head)
{
ListNode* tmp = head;
head = head->next;
tmp->next = result->next;
result->next = tmp;
}
return result->next;
}
};
递归
比较难理解,这里给的是别人的题解
/** * 以链表1->2->3->4->5举例 */
public ListNode* reverseList(ListNode* head) {
if (head == null || head.next == null) {
/* 直到当前节点的下一个节点为空时返回当前节点 由于5没有下一个节点了,所以此处返回节点5 */
return head;
}
//递归传入下一个节点,目的是为了到达最后一个节点
ListNode* newHead = reverseList(head.next);
/* 第一轮出栈,head为5,head.next为空,返回5 第二轮出栈,head为4,head.next为5,执行head.next.next=head也就是5.next=4, 把当前节点的子节点的子节点指向当前节点 此时链表为1->2->3->4<->5,由于4与5互相指向,所以此处要断开4.next=null 此时链表为1->2->3->4<-5 返回节点5 第三轮出栈,head为3,head.next为4,执行head.next.next=head也就是4.next=3, 此时链表为1->2->3<->4<-5,由于3与4互相指向,所以此处要断开3.next=null 此时链表为1->2->3<-4<-5 返回节点5 第四轮出栈,head为2,head.next为3,执行head.next.next=head也就是3.next=2, 此时链表为1->2<->3<-4<-5,由于2与3互相指向,所以此处要断开2.next=null 此时链表为1->2<-3<-4<-5 返回节点5 第五轮出栈,head为1,head.next为2,执行head.next.next=head也就是2.next=1, 此时链表为1<->2<-3<-4<-5,由于1与2互相指向,所以此处要断开1.next=null 此时链表为1<-2<-3<-4<-5 返回节点5 出栈完成,最终头节点5->4->3->2->1 */
head.next.next = head;
head.next = null;
return newHead;
}
边栏推荐
- 【每日一道LeetCode】——9. 回文数
- Nanoprobes纳米探针丨Nanogold偶联物的特点和应用
- Install mysql using docker
- OC和Swift语言的区别
- 树链剖分-
- 53. 最小的k个数
- 1688以图搜货
- 20. 用两个栈实现队列
- Pinduoduo leverages the consumer expo to promote the upgrading of domestic agricultural products brands and keep pace with international high-quality agricultural products
- OC中成员变量,实例变量和属性之间的区别和联系
猜你喜欢

局部敏感哈希:如何在常数时间内搜索Embedding最近邻

Outsourcing worked for three years, it was abolished...

The failure to create a role in Dahua Westward Journey has been solved

Nacos源码分析专题(二)-服务注册

Nanoprobes丨1-mercapto-(triethylene glycol) methyl ether functionalized gold nanoparticles

【Unity入门计划】2D Game Kit:初步了解2D游戏组成

Docker-compose安装mysql

指针数组和数组指针

Nanoprobes多组氨酸 (His-) 标签标记:重组蛋白检测方案

Remember a pit for gorm initialization
随机推荐
Electronic Manufacturing Warehouse Barcode Management System Solution
The first time I wrote a programming interview question for Niu Ke: input a string and return the letter with the most occurrences of the string
【web】Understanding Cookie and Session Mechanism
Reflex WMS Intermediate Series 7: What should I do if I want to cancel the picking of an HD that has finished picking but has not yet been loaded?
MySQL - CRUD operations
Lombok
leetcode / anagram in string - some permutation of s1 string is a substring of s2
The failure to create a role in Dahua Westward Journey has been solved
【每日一道LeetCode】——9. 回文数
机器人领域期刊会议汇总
数值积分方法:欧拉积分、中点积分和龙格-库塔法积分
MySQL索引优化实战
Nanoprobes Polyhistidine (His-) Tag: Recombinant Protein Detection Protocol
考完PMP学什么?前方软考等着你~
analog IC layout-Environmental noise
【CNN记录】tensorflow slice和strided_slice
Nanoprobes丨1-巯基-(三甘醇)甲醚功能化金纳米颗粒
A good book for newcomers to the workplace
永磁同步电机36问(三)——SVPWM代码实现
Good News | AR opens a new model for the textile industry, and ALVA Systems wins another award!