当前位置:网站首页>剑指 Offer 24. 反转链表
剑指 Offer 24. 反转链表
2022-07-02 14:21:00 【anieoo】
原题链接:剑指 Offer 24. 反转链表
solution:
直接遍历链表:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
ListNode *cur = head;
ListNode *pre = NULL;
while(cur != NULL) {
ListNode *tmp = cur->next;
cur->next = pre;
pre = cur;
cur = tmp;
}
return pre;
}
};
递归:
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode(int x) : val(x), next(NULL) {}
* };
*/
class Solution {
public:
ListNode* reverseList(ListNode* head) {
if(head == NULL || head->next == NULL) return head;
ListNode *tail = reverseList(head->next);
head->next->next = head;
head->next = NULL;
return tail;
}
};
边栏推荐
- LeetCode 5. Longest Palindromic Substring
- Kubernetes three open interfaces first sight
- Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
- Detailed explanation of @accessories annotation of Lombok plug-in
- Exploration of mobile application performance tools
- Atcoder beginer contest 169 (B, C, D unique decomposition, e mathematical analysis f (DP))
- PWM breathing lamp
- PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
- System Verilog implements priority arbiter
- VMware安装win10镜像
猜你喜欢
宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权
A week of short video platform 30W exposure, small magic push helps physical businesses turn losses into profits
john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
只是巧合?苹果iOS16的神秘技术竟然与中国企业5年前产品一致!
[error record] the connection of the flutter device shows loading (disconnect | delete the shuttle/bin/cache/lockfile file)
上传代码到远程仓库报错error: remote origin already exists.
【Leetcode】13. 罗马数字转整数
相信自己,这次一把搞定JVM面试
What is normal distribution? What is the 28 law?
社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
随机推荐
In MySQL and Oracle, the boundary and range of between and precautions when querying the date
电脑自带软件使图片底色变为透明(抠图白底)
学习周刊-总第60期-2022年第25周
P6774 [NOI2020] 时代的眼泪(分块)
Role and function of uboot
上传代码到远程仓库报错error: remote origin already exists.
Global and Chinese market of switching valves 2022-2028: Research Report on technology, participants, trends, market size and share
lsf基础命令
【Leetcode】14. 最长公共前缀
[leetcode] 14. Préfixe public le plus long
2322. 从树中删除边的最小分数(异或和&模拟)
How to choose the right kubernetes storage plug-in? (09)
OpenPose的使用
福元医药上交所上市:市值105亿 胡柏藩身价超40亿
Global and Chinese markets for disposable insulin pumps 2022-2028: Research Report on technology, participants, trends, market size and share
R and rstudio download and installation tutorial (super detailed)
VMware安装win10镜像
JS delete substring in string
Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
PWM breathing lamp