当前位置:网站首页>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;
}
}
边栏推荐
- [antd] how to set antd in form There is input in item Get input when gourp Value of each input of gourp
- 【C 题集】of Ⅶ
- How to package QT and share exe
- Read excel table data
- 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
- sharding key type not supported
- Understand chisel language thoroughly 10. Chisel project construction, operation and testing (II) -- Verilog code generation in chisel & chisel development process
- 吃透Chisel语言.11.Chisel项目构建、运行和测试(三)——Chisel测试之ScalaTest
- 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
- 去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
猜你喜欢
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
安装Mysql
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
[antd step pit] antd form cooperates with input Form The height occupied by item is incorrect
Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
markdown 语法之字体标红
The font of markdown grammar is marked in red
DDD application and practice of domestic hotel transactions -- Code
吃透Chisel语言.12.Chisel项目构建、运行和测试(四)——Chisel测试之ChiselTest
Why should Base64 encoding be used for image transmission
随机推荐
Applet live + e-commerce, if you want to be a new retail e-commerce, use it!
吃透Chisel语言.03.写给Verilog转Chisel的开发者(没有Verilog基础也可以看看)
sql优化之查询优化器
富文本编辑:wangEditor使用教程
自主工业软件的创新与发展
去除重复字母[贪心+单调栈(用数组+len来维持单调序列)]
php 日志调试
Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT
Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)
Install and use MAC redis, connect to remote server redis
Use of tiledlayout function in MATLAB
GCC [6] - 4 stages of compilation
2022游戏出海实用发行策略
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
【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
Introducing testfixture into unittest framework
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
Unity Shader学习(三)试着绘制一个圆
IDEA快捷键大全