当前位置:网站首页>剑指 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;
}
};边栏推荐
- What will you do after digital IC Verification?
- Résumé de l'entrevue de Dachang Daquan
- OpenHarmony如何启动远程设备的FA
- Xiaopeng P7 had an accident on rainy days, and the airbag did not pop up. Official response: the impact strength did not meet the ejection requirements
- john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
- Kubernetes three open interfaces first sight
- TCP server communication process (important)
- 深度学习图像数据自动标注[通俗易懂]
- Domestic relatively good OJ platform [easy to understand]
- 机器学习-感知机模型
猜你喜欢

LeetCode 1. 两数之和

unity Hub 登錄框變得很窄 無法登錄

Résumé de l'entrevue de Dachang Daquan
![john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])](/img/4c/ddf7f8085257d0eb8766dbec251345.png)
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])

Exploration and practice of integration of streaming and wholesale in jd.com

Masa framework - DDD design (1)

Ap和F107数据来源及处理

LeetCode 1. Sum of two numbers

Exploration of mobile application performance tools

串口控制舵机转动
随机推荐
Executive engine module of high performance data warehouse practice based on Impala
Kubernetes three open interfaces first sight
你想要的宏基因组-微生物组知识全在这(2022.7)
Interview summary of large factories
R and rstudio download and installation tutorial (super detailed)
[fluent] dart data type list set type (define set | initialize | generic usage | add elements after initialization | set generation function | set traversal)
移动应用性能工具探索之路
Seven charts, learn to do valuable business analysis
< IV & gt; H264 decode output YUV file
大廠面試總結大全
[cloud native] briefly talk about the understanding of flume, a massive data collection component
数字IC手撕代码--投票表决器
VMware安装win10镜像
二、mock平台的扩展
TCP congestion control details | 2 background
Understand one article: four types of data index system
TCP server communication process (important)
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
Cell: Tsinghua Chenggong group revealed an odor of skin flora. Volatiles promote flavivirus to infect the host and attract mosquitoes
C语言自定义函数的方法