当前位置:网站首页>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;
}
};边栏推荐
- Vscode setting delete line shortcut [easy to understand]
- Rock PI Development Notes (II): start with rock PI 4B plus (based on Ruixing micro rk3399) board and make system operation
- Executive engine module of high performance data warehouse practice based on Impala
- System Verilog implements priority arbiter
- PhD Debate-11 预告 | 回顾与展望神经网络的后门攻击与防御
- What will you do after digital IC Verification?
- 871. 最低加油次数
- System Verilog实现优先级仲裁器
- Day 18 of leetcode dynamic planning introduction
- 几行代码搞定RPC服务注册和发现
猜你喜欢
随机推荐
基于多元时间序列对高考预测分析案例
对接保时捷及3PL EDI案例
深度学习图像数据自动标注[通俗易懂]
Atcoder beginer contest 169 (B, C, D unique decomposition, e mathematical analysis f (DP))
Soul, a social meta universe platform, rushed to Hong Kong stocks: Tencent is a shareholder with an annual revenue of 1.28 billion
你想要的宏基因组-微生物组知识全在这(2022.7)
DGraph: 大规模动态图数据集
剑指 Offer 25. 合并两个排序的链表
剑指 Offer 22. 链表中倒数第k个节点
一年頂十年
Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion
A case study of college entrance examination prediction based on multivariate time series
Interpretation of key parameters in MOSFET device manual
< IV & gt; H264 decode output YUV file
Geoserver: publishing PostGIS data sources
Learning Weekly - total issue 60 - 25th week of 2022
What is the difference between JSP and servlet?
畅玩集团冲刺港股:年营收2.89亿 刘辉有53.46%投票权
MOSFET器件手册关键参数解读
【Leetcode】14. Longest Common Prefix








