当前位置:网站首页>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;
}
}
边栏推荐
- What is the difference between Bi financial analysis in a narrow sense and financial analysis in a broad sense?
- Learn kernel 3: use GDB to track the kernel call chain
- 迅为IMX6Q开发板QT系统移植tinyplay
- MySQL的存储过程练习题
- Ruiji takeout notes
- nowcoder重排链表
- One architecture to complete all tasks - transformer architecture is unifying the AI Jianghu on its own
- redis 日常笔记
- [FAQ] summary of common causes and solutions of Huawei account service error 907135701
- 潘多拉 IOT 开发板学习(RT-Thread)—— 实验3 按键实验(学习笔记)
猜你喜欢
失败率高达80%,企业数字化转型路上有哪些挑战?
sharding key type not supported
【MySQL从入门到精通】【高级篇】(四)MySQL权限管理与控制
Intelligence d'affaires bi analyse financière, analyse financière au sens étroit et analyse financière au sens large sont - ils différents?
Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
Remove duplicate letters [greedy + monotonic stack (maintain monotonic sequence with array +len)]
92.(cesium篇)cesium楼栋分层
为什么图片传输要使用base64编码
学内核之三:使用GDB跟踪内核调用链
Real time data warehouse
随机推荐
Rich text editing: wangeditor tutorial
vscode 常用插件汇总
迅为IMX6Q开发板QT系统移植tinyplay
ARouter的使用
What is the real meaning and purpose of doing things, and what do you really want
Understand chisel language thoroughly 11. Chisel project construction, operation and test (III) -- scalatest of chisel test
redis 日常笔记
Leetcode T48:旋转图像
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
Use of arouter
【云原生】我怎么会和这个数据库杠上了?
nowcoder重排链表
Understand chisel language thoroughly 09. Chisel project construction, operation and testing (I) -- build and run chisel project with SBT
10.(地图数据篇)离线地形数据处理(供Cesium使用)
Use of tiledlayout function in MATLAB
聊聊保证线程安全的 10 个小技巧
【算法leetcode】面试题 04.03. 特定深度节点链表(多语言实现)
Why should Base64 encoding be used for image transmission
流行框架:Glide的使用