当前位置:网站首页>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;
}
}
边栏推荐
- Excel quickly merges multiple rows of data
- LifeCycle
- 递增的三元子序列[贪心训练]
- 基于YOLOv1的口罩佩戴检测
- Mask wearing detection based on yolov1
- sharding key type not supported
- 奇妙秘境 码蹄集
- 吃透Chisel语言.07.Chisel基础(四)——Bundle和Vec
- IP 实验室月复盘 · 第 5 期
- Apple 5g chip research and development failure: continue to rely on Qualcomm, but also worry about being prosecuted?
猜你喜欢

Vscode common plug-ins summary
![去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]](/img/af/a1dcba6f45eb4ccc668cd04a662e9c.png)
去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]

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

【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法

Data warehouse interview question preparation

Test evaluation of software testing

吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest

MATLAB中tiledlayout函数使用

【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法

Hardware Basics - diode Basics
随机推荐
R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据
Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT
File creation, writing, reading, deletion (transfer) in go language
Gorm read / write separation (rotation)
吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
go vendor 项目迁移到 mod 项目
安装Mysql
[R language data science]: cross validation and looking back
富文本编辑:wangEditor使用教程
392. Judgement subsequence
卷积神经网络经典论文集合(深度学习分类篇)
小程序直播 + 电商,想做新零售电商就用它吧!
GCC【6】- 编译的4个阶段
The font of markdown grammar is marked in red
Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test
Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
游戏出海,全球化运营
golang fmt. Printf() (turn)
LifeCycle
[antd] how to set antd in form There is input in item Get input when gourp Value of each input of gourp