当前位置:网站首页>Sword finger offer 24 Reverse linked list
Sword finger offer 24 Reverse linked list
2022-07-02 17:12:00 【anieoo】
Original link : The finger of the sword Offer 24. Reverse a linked list
solution:
Direct traversal of linked list :
/**
* 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;
}
};
recursive :
/**
* 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;
}
};
边栏推荐
- Error when uploading code to remote warehouse: remote origin already exists
- 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
- 移动应用性能工具探索之路
- OpenPose的使用
- P6774 [noi2020] tears in the era (block)
- 你想要的宏基因组-微生物组知识全在这(2022.7)
- 有赞和腾讯云、阿里云一同摘得“中国企业云科技服务商50强”[通俗易懂]
- Masa framework - DDD design (1)
- lsf基础命令
- 超卓航科上市:募资9亿市值超60亿 成襄阳首家科创板企业
猜你喜欢
【Leetcode】14. 最长公共前缀
PWM controlled steering gear
博客主题 “Text“ 夏日清新特别版
PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
Executive engine module of high performance data warehouse practice based on Impala
Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
亚马逊云科技 Community Builder 申请窗口开启
电脑自带软件使图片底色变为透明(抠图白底)
13、Darknet YOLO3
上传代码到远程仓库报错error: remote origin already exists.
随机推荐
Easy language ABCD sort
Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
学习周刊-总第60期-2022年第25周
DigiCert SSL证书支持中文域名申请吗?
Method of C language self defining function
One year is worth ten years
pwm呼吸燈
Exploration and practice of integration of streaming and wholesale in jd.com
C语言自定义函数的方法
Masa framework - DDD design (1)
【Leetcode】13. 罗马数字转整数
john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
Day 18 of leetcode dynamic planning introduction
PWM breathing lamp
智能垃圾桶(五)——点亮OLED
Leetcode1380: lucky numbers in matrix
[leetcode] 14. Préfixe public le plus long
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
uboot的作用和功能
go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)