当前位置:网站首页>nowcoder重排链表
nowcoder重排链表
2022-07-04 12:51:00 【范谦之】
思路
在 O ( n ) O(n) O(n) 的空间复杂度要求下,很容易想到用顺序表存储所有的结点。
若空间复杂度为 O ( 1 ) O(1) O(1):
- 使用快慢指针找到中间结点。
- 反转后半段链表
- 合并两段链表
代码
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;
}
public void reorderList(ListNode head) {
ListNode fast = head;
ListNode slow = head;
while(fast.next!=null && fast.next.next!=null) {
slow = slow.next;
fast = fast.next.next;
}
ListNode rrh = reverseList(slow.next);
slow.next = null;
ListNode lh = head;
while(rrh!=null) {
ListNode rrh_next = rrh.next, lh_next = lh.next;
rrh.next = lh.next;
lh.next = rrh;
lh = lh_next;
rrh = rrh_next;
}
}
边栏推荐
- LifeCycle
- Excel快速合并多行数据
- Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
- PHP log debugging
- Haobo medical sprint technology innovation board: annual revenue of 260million Yonggang and Shen Zhiqun are the actual controllers
- MATLAB中tiledlayout函数使用
- 【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法
- R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
- Test process arrangement (3)
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
猜你喜欢
Qt如何实现打包,实现EXE分享
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
吃透Chisel语言.11.Chisel项目构建、运行和测试(三)——Chisel测试之ScalaTest
Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million
Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test
1200. Minimum absolute difference
吃透Chisel语言.06.Chisel基础(三)——寄存器和计数器
Use of tiledlayout function in MATLAB
sql优化之explain
随机推荐
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
Can mortgage with housing exclude compulsory execution
Innovation and development of independent industrial software
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
Xcode 异常图片导致ipa包增大问题
IP lab monthly resumption · issue 5
xshell/bash/zsh 等终端鼠标滚轮乱码问题(转)
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
吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
Unittest框架中引入TestFixture
安装Mysql
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据
LifeCycle
mac redis安装与使用,连接远程服务器 redis
Unity Shader学习(三)试着绘制一个圆
R language uses the DOTPLOT function of epidisplay package to visualize the frequency of data points in different intervals in the form of point graph, and uses the by parameter to specify the groupin
R language uses the mutation function of dplyr package to standardize the specified data column (using mean function and SD function), and calculates the grouping mean of the standardized target varia
C# wpf 实现截屏框实时截屏功能
C # WPF realizes the real-time screen capture function of screen capture box