当前位置:网站首页>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;
}
边栏推荐
- Understand chisel language thoroughly 08. Chisel Foundation (V) -- wire, REG and IO, and how to understand chisel generation hardware
- R语言ggplot2可视化:gganimate包创建动画图(gif)、使用anim_save函数保存gif可视化动图
- 【C 题集】of Ⅶ
- 富文本编辑:wangEditor使用教程
- 2022游戏出海实用发行策略
- R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
- IP lab monthly resumption · issue 5
- Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
- The font of markdown grammar is marked in red
- Excel quickly merges multiple rows of data
猜你喜欢
瑞吉外卖笔记
10.(地图数据篇)离线地形数据处理(供Cesium使用)
DDD application and practice of domestic hotel transactions -- Code
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Understand chisel language thoroughly 10. Chisel project construction, operation and testing (II) -- Verilog code generation in chisel & chisel development process
Innovation and development of independent industrial software
Unittest框架中引入TestFixture
sharding key type not supported
软件测试之测试评估
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
随机推荐
程序员的焦虑
qt 怎么检测鼠标在不在某个控件上
R语言使用dplyr包的mutate函数对指定数据列进行标准化处理(使用mean函数和sd函数)并基于分组变量计算标准化后的目标变量的分组均值
学内核之三:使用GDB跟踪内核调用链
LifeCycle
CVPR 2022 | greatly reduce the manual annotation required for zero sample learning, and propose category semantic embedding rich in visual information (source code download)
富文本编辑:wangEditor使用教程
Common content type correspondence table
IP lab monthly resumption · issue 5
Whether the loyalty agreement has legal effect
92.(cesium篇)cesium楼栋分层
Read excel table data
The font of markdown grammar is marked in red
LiveData
去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
Error in find command: paths must precede expression (turn)
golang fmt. Printf() (turn)
10.(地图数据篇)离线地形数据处理(供Cesium使用)
Can mortgage with housing exclude compulsory execution
吃透Chisel语言.04.Chisel基础(一)——信号类型和常量