当前位置:网站首页>剑指 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;
}
};边栏推荐
猜你喜欢

TCP congestion control details | 2 background

go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)

Serial port controls steering gear rotation

DigiCert SSL证书支持中文域名申请吗?

Machine learning perceptron model
![L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])](/img/4c/ddf7f8085257d0eb8766dbec251345.png)
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])

【Leetcode】13. 罗马数字转整数

Dgraph: large scale dynamic graph dataset

Notice on holding a salon for young editors of scientific and Technological Journals -- the abilities and promotion strategies that young editors should have in the new era

What is normal distribution? What is the 28 law?
随机推荐
社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
Method of C language self defining function
寒门再出贵子:江西穷县考出了省状元,做对了什么?
La boîte de connexion du hub de l'unit é devient trop étroite pour se connecter
MOSFET器件手册关键参数解读
TCP server communication process (important)
Learning Weekly - total issue 60 - 25th week of 2022
Résumé de l'entrevue de Dachang Daquan
隐私计算技术创新及产业实践研讨会:学习
Yyds dry inventory uses thread safe two-way linked list to realize simple LRU cache simulation
LeetCode 6. Zigzag transformation (n-shaped transformation)
Penetration tool - intranet permission maintenance -cobalt strike
john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
jsp 和 servlet 有什么区别?
你想要的宏基因组-微生物组知识全在这(2022.7)
How to choose the right kubernetes storage plug-in? (09)
相信自己,这次一把搞定JVM面试
DGraph: 大规模动态图数据集
AP and F107 data sources and processing
[leetcode] 14. Préfixe public le plus long