当前位置:网站首页>Nowcoder rearrange linked list
Nowcoder rearrange linked list
2022-07-04 14:24:00 【Fan Qianzhi】
Ideas
stay O ( n ) O(n) O(n) Under the space complexity requirements of , It's easy to think of using a sequence table to store all nodes .
If the space complexity is O ( 1 ) O(1) O(1):
- Use the speed pointer to find the intermediate node .
- Reverse the second half of the list
- Merge two linked lists
Code
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;
}
}
边栏推荐
- R语言ggplot2可视化:gganimate包创建动态折线图动画(gif)、使用transition_reveal函数在动画中沿给定维度逐步显示数据
- LifeCycle
- Golang uses JSON unmarshal number to interface{} number to become float64 type (turn)
- 去除重複字母[貪心+單調棧(用數組+len來維持單調序列)]
- Rich text editing: wangeditor tutorial
- R语言使用dplyr包的group_by函数和summarise函数基于分组变量计算目标变量的均值、标准差
- What is the real meaning and purpose of doing things, and what do you really want
- What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
- Understand chisel language thoroughly 05. Chisel Foundation (II) -- combinational circuits and operators
- One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own
猜你喜欢
【信息检索】链接分析
实时数据仓库
【信息检索】分类和聚类的实验
Product identification of intelligent retail cabinet based on paddlex
使用CLion编译OGLPG-9th-Edition源码
Understand chisel language thoroughly 12. Chisel project construction, operation and testing (IV) -- chisel test of chisel test
商業智能BI財務分析,狹義的財務分析和廣義的財務分析有何不同?
数据中台概念
Use of tiledlayout function in MATLAB
TestSuite and testrunner in unittest
随机推荐
数据仓库面试问题准备
AI and Life Sciences
Use of tiledlayout function in MATLAB
实战解惑 | OpenCV中如何提取不规则ROI区域
sharding key type not supported
[matlab] summary of conv, filter, conv2, Filter2 and imfilter convolution functions
Product identification of intelligent retail cabinet based on paddlex
File creation, writing, reading, deletion (transfer) in go language
Install MySQL
Understand chisel language thoroughly 07. Chisel Foundation (IV) - bundle and VEC
Install and use MAC redis, connect to remote server redis
Incremental ternary subsequence [greedy training]
Popular framework: the use of glide
实时数据仓库
leetcode:6110. 网格图中递增路径的数目【dfs + cache】
如何游戏出海代运营、游戏出海代投
R language dplyr package summary_ If function calculates the mean and median of all numerical data columns in dataframe data, and summarizes all numerical variables based on conditions
数据埋点的一些问题和想法
The failure rate is as high as 80%. What are the challenges on the way of enterprise digital transformation?
一文概览2D人体姿态估计