当前位置:网站首页>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;
}
}
边栏推荐
- Unittest框架中引入TestFixture
- Basic mode of service mesh
- DDD application and practice of domestic hotel transactions -- Code
- 小程序直播 + 电商,想做新零售电商就用它吧!
- R语言dplyr包summarise_if函数计算dataframe数据中所有数值数据列的均值和中位数、基于条件进行数据汇总分析(Summarize all Numeric Variables)
- Detailed index of MySQL
- Excel快速合并多行数据
- 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
- sharding key type not supported
- Assertion of unittest framework
猜你喜欢

Test evaluation of software testing
![递增的三元子序列[贪心训练]](/img/92/7efd1883c21c0e804ffccfb2231602.png)
递增的三元子序列[贪心训练]

DDD application and practice of domestic hotel transactions -- Code

10.(地图数据篇)离线地形数据处理(供Cesium使用)

Why should Base64 encoding be used for image transmission

测试流程整理(2)

Understand chisel language thoroughly 10. Chisel project construction, operation and testing (II) -- Verilog code generation in chisel & chisel development process

Understand chisel language thoroughly 06. Chisel Foundation (III) -- registers and counters

Excel quickly merges multiple rows of data
![[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions](/img/7a/9b559313b407f9a12cbaed7bebd4dc.png)
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
随机推荐
软件测试之测试评估
Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million
go语言中的文件创建,写入,读取,删除(转)
Understand chisel language thoroughly 03. Write to the developer of Verilog to chisel (you can also see it without Verilog Foundation)
LiveData
MySQL 5 installation and modification free
Innovation and development of independent industrial software
安装Mysql
瑞吉外卖笔记
Understand chisel language thoroughly 07. Chisel Foundation (IV) - bundle and VEC
[FAQ] summary of common causes and solutions of Huawei account service error 907135701
Understand chisel language thoroughly 06. Chisel Foundation (III) -- registers and counters
The mouse wheel of xshell/bash/zsh and other terminals is garbled (turn)
Unittest框架中引入TestFixture
markdown 语法之字体标红
基于PaddleX的智能零售柜商品识别
Whether the loyalty agreement has legal effect
Understand chisel language thoroughly 08. Chisel Foundation (V) -- wire, REG and IO, and how to understand chisel generation hardware
Qt如何实现打包,实现EXE分享
Test evaluation of software testing