当前位置:网站首页>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;
}
};边栏推荐
猜你喜欢

LeetCode 1. Sum of two numbers

How openharmony starts FA of remote devices

酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东

Amazon cloud technology community builder application window opens

深度之眼(二)——矩阵及其基本运算

亚马逊云科技 Community Builder 申请窗口开启

2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition (a sign in, B sign in, C sign in, D thinking +mst

Penetration tool - intranet permission maintenance -cobalt strike
![L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])](/img/4c/ddf7f8085257d0eb8766dbec251345.png)
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])

Ap和F107数据来源及处理
随机推荐
一年顶十年
DigiCert SSL证书支持中文域名申请吗?
有赞和腾讯云、阿里云一同摘得“中国企业云科技服务商50强”[通俗易懂]
MOSFET器件手册关键参数解读
One year is worth ten years
System Verilog implements priority arbiter
P6774 [noi2020] tears in the era (block)
Easy language ABCD sort
Error when uploading code to remote warehouse: remote origin already exists
L'explosion de John utilise l'encodage d'entrée par défaut: UTF - 8 Loaded 1 password Hash (bcrypt [blowfish 32 / 64 X3])
Leetcode1380: lucky numbers in matrix
Kubernetes three open interfaces first sight
The computer comes with software to make the background color of the picture transparent (matting white background)
Usage of sprintf() function in C language
Cell: Tsinghua Chenggong group revealed an odor of skin flora. Volatiles promote flavivirus to infect the host and attract mosquitoes
二、mock平台的扩展
宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权
【Leetcode】13. 罗马数字转整数
OpenPose的使用
JS delete substring in string