当前位置:网站首页>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;
}
}
边栏推荐
- WS2818M是CPC8封装,是三通道LED驱动控制专用电路外置IC全彩双信号5V32灯可编程led灯带户外工程
- LifeCycle
- 小程序直播 + 电商,想做新零售电商就用它吧!
- [R language data science]: cross validation and looking back
- 【信息检索】分类和聚类的实验
- Whether the loyalty agreement has legal effect
- BLOB,TEXT GEOMETRY or JSON column 'xxx' can't have a default value query 问题
- R语言使用epiDisplay包的dotplot函数通过点图的形式可视化不同区间数据点的频率、使用by参数指定分组参数可视化不同分组的点图分布
- LiveData
- Understand chisel language thoroughly 08. Chisel Foundation (V) -- wire, REG and IO, and how to understand chisel generation hardware
猜你喜欢
How to package QT and share exe
92.(cesium篇)cesium楼栋分层
Data warehouse interview question preparation
Yingshi Ruida rushes to the scientific and Technological Innovation Board: the annual revenue is 450million and the proposed fund-raising is 979million
Ruichengxin micro sprint technology innovation board: annual revenue of 367million, proposed to raise 1.3 billion, Datang Telecom is a shareholder
[FAQ] Huawei Account Service Error Report 907135701 Common reasons Summary and Solutions
markdown 语法之字体标红
392. Judgement subsequence
Use the default route as the route to the Internet
TestSuite and testrunner in unittest
随机推荐
Use of tiledlayout function in MATLAB
Can mortgage with housing exclude compulsory execution
MySQL 5 installation and modification free
【FAQ】华为帐号服务报错 907135701的常见原因总结和解决方法
Test process arrangement (3)
TestSuite and testrunner in unittest
【信息检索】分类和聚类的实验
92.(cesium篇)cesium楼栋分层
Excel快速合并多行数据
Idea shortcut keys
Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test
测试流程整理(3)
Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
sql优化之explain
学习项目是自己找的,成长机会是自己创造的
卷积神经网络经典论文集合(深度学习分类篇)
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
GCC【6】- 编译的4个阶段
【Matlab】conv、filter、conv2、filter2和imfilter卷积函数总结
How to package QT and share exe