当前位置:网站首页>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;
}
};边栏推荐
- Executive engine module of high performance data warehouse practice based on Impala
- 【Leetcode】13. 罗马数字转整数
- uboot的作用和功能
- System Verilog实现优先级仲裁器
- 入行数字IC验证后会做些什么?
- Use the API port of the bridge of knowledge and action to provide resources for partners to access
- 亚马逊云科技 Community Builder 申请窗口开启
- go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)
- VMware install win10 image
- 【Leetcode】13. Roman numeral to integer
猜你喜欢

TCP congestion control details | 2 background

基于多元时间序列对高考预测分析案例

Seal Library - installation and introduction

go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)

DGraph: 大规模动态图数据集

移动应用性能工具探索之路

智能垃圾桶(五)——点亮OLED
![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])

Exploration and practice of integration of streaming and wholesale in jd.com
![[error record] error -32000 received from application: there are no running service protocol](/img/6c/66099650de46cac88b805e6cfb90b9.jpg)
[error record] error -32000 received from application: there are no running service protocol
随机推荐
体验居家办公完成项目有感 | 社区征文
默认浏览器设置不了怎么办?
Serial port controls steering gear rotation
Detailed explanation of @accessories annotation of Lombok plug-in
How to transfer business data with BorgWarner through EDI?
七张图,学会做有价值的经营分析
IP地址转换地址段
Easy language ABCD sort
Error when uploading code to remote warehouse: remote origin already exists
john爆破出现Using default input encoding: UTF-8 Loaded 1 password hash (bcrypt [Blowfish 32/64 X3])
<四> H264解码输出yuv文件
Domestic relatively good OJ platform [easy to understand]
配置基于接口的ARP表项限制和端口安全(限制用户私自接入傻瓜交换机或非法主机接入)
A case study of college entrance examination prediction based on multivariate time series
LeetCode 1. Sum of two numbers
linux安装postgresql + patroni 集群问题
Does digicert SSL certificate support Chinese domain name application?
VMware安装win10镜像
Digital IC hand tearing code -- voting device
Yolov5 practice: teach object detection by hand