当前位置:网站首页>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;
}
}
边栏推荐
- leetcode:6110. 网格图中递增路径的数目【dfs + cache】
- The game goes to sea and operates globally
- R language uses bwplot function in lattice package to visualize box plot and par Settings parameter custom theme mode
- golang fmt. Printf() (turn)
- Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
- 【信息检索】分类和聚类的实验
- Blob, text geometry or JSON column'xxx'can't have a default value query question
- File creation, writing, reading, deletion (transfer) in go language
- Map of mL: Based on Boston house price regression prediction data set, an interpretable case is realized by using the map value to the LIR linear regression model
- 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
猜你喜欢
Mask wearing detection based on yolov1
Data center concept
测试流程整理(3)
Detailed index of MySQL
Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
Install MySQL
聊聊保证线程安全的 10 个小技巧
92.(cesium篇)cesium楼栋分层
Product identification of intelligent retail cabinet based on paddlex
nowcoder重排链表
随机推荐
数据湖(十三):Spark与Iceberg整合DDL操作
Assertion of unittest framework
GCC [6] - 4 stages of compilation
【信息检索】链接分析
Idea shortcut keys
【FAQ】華為帳號服務報錯 907135701的常見原因總結和解决方法
nowcoder重排链表
Gorm data insertion (transfer)
Some problems and ideas of data embedding point
Mongodb commonly used 28 query statements (forward)
Learning projects are self-made, and growth opportunities are self created
2022游戏出海实用发行策略
The font of markdown grammar is marked in red
商業智能BI財務分析,狹義的財務分析和廣義的財務分析有何不同?
ML之shap:基于boston波士顿房价回归预测数据集利用shap值对XGBoost模型实现可解释性案例
Error in find command: paths must precede expression (turn)
统计php程序运行时间及设置PHP最长运行时间
Migration from go vendor project to mod project
The game goes to sea and operates globally
Test process arrangement (2)