当前位置:网站首页>NowCoder 反转链表
NowCoder 反转链表
2022-07-04 12:51:00 【范谦之】
描述
给定一个单链表的头结点pHead(该头节点是有值的,比如在下图,它的val是1),长度为n,反转该链表后,返回新链表的表头。
数据范围: 0 ≤ n ≤ 10000 0\leq n\leq10000 0≤n≤10000
要求:空间复杂度 O(1),时间复杂度 O(n)。
如当输入链表{1,2,3}时,
经反转后,原链表变为{3,2,1},所以对应的输出为{3,2,1}。
以上转换过程如下图所示:

思路
使用递归方法反转链表。
代码
public ListNode ReverseList(ListNode head) {
if (head == null || head.next == null)
return head;
ListNode reverse = ReverseList(head.next);
head.next.next = head;
head.next = null;
return reverse;
}
边栏推荐
- Test process arrangement (3)
- QT how to detect whether the mouse is on a control
- R语言使用dplyr包的mutate函数对指定数据列进行标准化处理(使用mean函数和sd函数)并基于分组变量计算标准化后的目标变量的分组均值
- Install MySQL
- C # WPF realizes the real-time screen capture function of screen capture box
- sharding key type not supported
- R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据
- mac redis安装与使用,连接远程服务器 redis
- Basic mode of service mesh
- 【信息检索】分类和聚类的实验
猜你喜欢

软件测试之测试评估

C # WPF realizes the real-time screen capture function of screen capture box

CVPR 2022 | greatly reduce the manual annotation required for zero sample learning, and propose category semantic embedding rich in visual information (source code download)

Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million

吃透Chisel语言.10.Chisel项目构建、运行和测试(二)——Chisel中生成Verilog代码&Chisel开发流程

Use of tiledlayout function in MATLAB

Introducing testfixture into unittest framework
![Incremental ternary subsequence [greedy training]](/img/92/7efd1883c21c0e804ffccfb2231602.png)
Incremental ternary subsequence [greedy training]

基于YOLOv1的口罩佩戴检测

MySQL之详解索引
随机推荐
LifeCycle
Introducing testfixture into unittest framework
Idea shortcut keys
Data warehouse interview question preparation
C# wpf 实现截屏框实时截屏功能
【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
【信息检索】链接分析
php 日志调试
JVM memory layout detailed, illustrated, well written!
File creation, writing, reading, deletion (transfer) in go language
Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test
CVPR 2022 | greatly reduce the manual annotation required for zero sample learning, and propose category semantic embedding rich in visual information (source code download)
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
2022 practice questions and mock exams for the main principals of hazardous chemical business units
Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)
sharding key type not supported
Unittest框架之断言
Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
递增的三元子序列[贪心训练]