当前位置:网站首页>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;
}
边栏推荐
- Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
- Unittest框架中引入TestFixture
- FS4059C是5V输入升压充电12.6V1.2A给三节锂电池充电芯片 输入小电流不会拉死,温度60°建议1000-1100MA
- Qt如何实现打包,实现EXE分享
- 程序员的焦虑
- MySQL之详解索引
- 吃透Chisel语言.09.Chisel项目构建、运行和测试(一)——用sbt构建Chisel项目并运行
- The game goes to sea and operates globally
- Hardware Basics - diode Basics
- IDEA快捷键大全
猜你喜欢
Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
瑞吉外卖笔记
gin集成支付宝支付
C # WPF realizes the real-time screen capture function of screen capture box
392. Judgement subsequence
富文本编辑:wangEditor使用教程
测试流程整理(3)
软件测试之测试评估
吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器
Unity Shader学习(三)试着绘制一个圆
随机推荐
Ws2818m is packaged in cpc8. It is a special circuit for three channel LED drive control. External IC full-color double signal 5v32 lamp programmable LED lamp with outdoor engineering
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
学内核之三:使用GDB跟踪内核调用链
Innovation and development of independent industrial software
吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器
C# wpf 实现截屏框实时截屏功能
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
Programmer anxiety
2022 practice questions and mock exams for the main principals of hazardous chemical business units
File creation, writing, reading, deletion (transfer) in go language
MySQL之详解索引
sql优化之explain
Migration from go vendor project to mod project
MATLAB中tiledlayout函数使用
卷积神经网络经典论文集合(深度学习分类篇)
Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million
游戏出海,全球化运营
php 日志调试
PHP log debugging
Huahao Zhongtian sprint Technology Innovation Board: perte annuelle de 280 millions de RMB, projet de collecte de fonds de 1,5 milliard de Beida Pharmaceutical est actionnaire