当前位置:网站首页>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;
}
};
边栏推荐
- P6774 [NOI2020] 时代的眼泪(分块)
- 超卓航科上市:募资9亿市值超60亿 成襄阳首家科创板企业
- 配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
- OpenPose的使用
- LeetCode 3. Longest substring without duplicate characters
- 【Leetcode】14. 最長公共前綴
- [leetcode] 14. Préfixe public le plus long
- 剑指 Offer 25. 合并两个排序的链表
- Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!
- C语言中sprintf()函数的用法
猜你喜欢
Day 18 of leetcode dynamic planning introduction
Geoserver: publishing PostGIS data sources
In MySQL and Oracle, the boundary and range of between and precautions when querying the date
Understand one article: four types of data index system
绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东
移动应用性能工具探索之路
Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
How to transfer business data with BorgWarner through EDI?
Tech Talk 活动预告 | 基于Amazon KVS打造智能视觉产品
寒门再出贵子:江西穷县考出了省状元,做对了什么?
随机推荐
OpenHarmony如何启动远程设备的FA
易语言abcd排序
Soul, a social meta universe platform, rushed to Hong Kong stocks: Tencent is a shareholder with an annual revenue of 1.28 billion
Geoserver: publishing PostGIS data sources
Hard core! One configuration center for 8 classes!
R and rstudio download and installation tutorial (super detailed)
Configure ARP table entry restrictions and port security based on the interface (restrict users' private access to fool switches or illegal host access)
Just a coincidence? The mysterious technology of apple ios16 is even consistent with the products of Chinese enterprises five years ago!
LeetCode 5. Longest Palindromic Substring
System Verilog implements priority arbiter
Penetration tool - intranet permission maintenance -cobalt strike
<四> H264解码输出yuv文件
QStyle实现自绘界面项目实战(二)
PWM breathing lamp
2、 Expansion of mock platform
宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权
酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
体验居家办公完成项目有感 | 社区征文
QWebEngineView崩溃及替代方案
一年頂十年