当前位置:网站首页>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;
}
};
边栏推荐
- Dgraph: large scale dynamic graph dataset
- Method of C language self defining function
- Yolov5 practice: teach object detection by hand
- 关于举办科技期刊青年编辑沙龙——新时代青年编辑应具备的能力及提升策略的通知...
- Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
- lsf基础命令
- PWM breathing lamp
- [cloud native] briefly talk about the understanding of flume, a massive data collection component
- Changwan group rushed to Hong Kong stocks: the annual revenue was 289million, and Liu Hui had 53.46% voting rights
- 相信自己,这次一把搞定JVM面试
猜你喜欢
基于Impala的高性能数仓实践之执行引擎模块
小鹏P7雨天出事故安全气囊没有弹出 官方回应:撞击力度未达到弹出要求
体验居家办公完成项目有感 | 社区征文
如何与博格华纳BorgWarner通过EDI传输业务数据?
Atcoder beginer contest 169 (B, C, D unique decomposition, e mathematical analysis f (DP))
Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品
博客主题 “Text“ 夏日清新特别版
PWM breathing lamp
The macrogenome microbiome knowledge you want is all here (2022.7)
DigiCert SSL证书支持中文域名申请吗?
随机推荐
LeetCode 5. Longest Palindromic Substring
入行数字IC验证后会做些什么?
默认浏览器设置不了怎么办?
綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東
The macrogenome microbiome knowledge you want is all here (2022.7)
【Leetcode】13. 罗马数字转整数
What will you do after digital IC Verification?
JS delete substring in string
PWM breathing lamp
Kubernetes three open interfaces first sight
Use the API port of the bridge of knowledge and action to provide resources for partners to access
上传代码到远程仓库报错error: remote origin already exists.
john爆破出現Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
LeetCode 3. Longest substring without duplicate characters
uboot的作用和功能
学习周刊-总第60期-2022年第25周
配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
七张图,学会做有价值的经营分析
人生的开始