当前位置:网站首页>Nowcoder reverse linked list
Nowcoder reverse linked list
2022-07-04 14:24:00 【Fan Qianzhi】
describe
Given the head node of a single linked list pHead( The header node has a value , For example, in the figure below , its val yes 1), The length is n, After reversing the linked list , Return the header of the new linked list .
Data range : 0 ≤ n ≤ 10000 0\leq n\leq10000 0≤n≤10000
requirement : Spatial complexity O(1), Time complexity O(n).
For example, when entering a linked list {1,2,3} when ,
After reversal , The original linked list becomes {3,2,1}, So the corresponding output is {3,2,1}.
The above conversion process is shown in the figure below :
Ideas
Reverse the linked list with recursive method .
Code
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;
}
边栏推荐
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- 去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
- 商业智能BI财务分析,狭义的财务分析和广义的财务分析有何不同?
- nowcoder重排链表
- What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
- 10.(地图数据篇)离线地形数据处理(供Cesium使用)
- codeforce:C. Sum of Substrings【边界处理 + 贡献思维 + 灵光一现】
- R language ggplot2 visualization: gganimate package creates animated graph (GIF) and uses anim_ The save function saves the GIF visual animation
- Learn kernel 3: use GDB to track the kernel call chain
- R language ggplot2 visualization: gganimate package creates dynamic line graph animation (GIF) and uses transition_ The reveal function displays data step by step along a given dimension in the animat
猜你喜欢
随机推荐
Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
Fs4059c is a 5V input boost charging 12.6v1.2a. Inputting a small current to three lithium battery charging chips will not pull it dead. The temperature is 60 ° and 1000-1100ma is recommended
LiveData
LifeCycle
卷积神经网络经典论文集合(深度学习分类篇)
C# wpf 实现截屏框实时截屏功能
Use of tiledlayout function in MATLAB
ARouter的使用
Product identification of intelligent retail cabinet based on paddlex
实战解惑 | OpenCV中如何提取不规则ROI区域
sql优化之查询优化器
Chapter 17 process memory
92.(cesium篇)cesium楼栋分层
RK1126平台OSD的实现支持颜色半透明度多通道支持中文
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
Matters needing attention in overseas game Investment Agency
Leetcode T47: 全排列II
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
Rich text editing: wangeditor tutorial
【MySQL从入门到精通】【高级篇】(四)MySQL权限管理与控制