当前位置:网站首页>leetcode 143. Rearrange linked list
leetcode 143. Rearrange linked list
2022-07-18 13:42:00 【henujolly】
class Solution {
public void reorderList(ListNode head) {
if (head == null) {
return;
}
ListNode mid = middleNode(head);
mergeList(head, reverseList(mid));
}
public ListNode middleNode(ListNode head) {
ListNode slow = head;
ListNode fast = head;
while (fast.next != null && fast.next.next != null) {
slow = slow.next;
fast = fast.next.next;
}
return slow;
}
public ListNode reverseList(ListNode head) {
ListNode prev = null;
ListNode curr = head;
while (curr != null) {
ListNode nextTemp = curr.next;
curr.next = prev;
prev = curr;
curr = nextTemp;
}
return prev;
}
public void mergeList(ListNode l1, ListNode l2) {
ListNode l1_tmp;
ListNode l2_tmp;
while (l1 != null && l2 != null) {
l1_tmp = l1.next;
l2_tmp = l2.next;
l1.next = l2;
l1 = l1_tmp;
l2.next = l1;
l2 = l2_tmp;
}
}
}
边栏推荐
- Sword finger offer 50 The first character that appears only once
- Advanced principle of MySQL: MySQL execution process and order
- [TinyML]NetAug:Network Augmentation for Tiny Deep Learning
- 视频处理:视频抽样
- 自动补全(春季每日一题 58)
- How to generate non repeated random numbers in Excel, multi method + principle
- Why does QT creator compile so slowly?
- Hcip the next day
- Hcip first day study -- Review HCIA (static)
- micro、M3O微服务系列(二)
猜你喜欢

Static routing technology

Dls-42/4-4 dc110v double position relay
![[deep learning] environment configuration of hands-on learning and deep learning](/img/71/059a5cd30f5f2ecd457fe67e2ae685.png)
[deep learning] environment configuration of hands-on learning and deep learning

ORA-19625异常处理记录

Quick use of jdbctemplate

Zhongang Mining: Fluorite guarantees the supply of fluorine in new energy industry

Exploratory software testing
![[independent station operation] understand the four skills of cross-border e-commerce operation in one minute!](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[independent station operation] understand the four skills of cross-border e-commerce operation in one minute!
![[step on the pit] resurrect Pico go](/img/5a/434813aec233c1f62aad952fbaace8.png)
[step on the pit] resurrect Pico go

Dual position relay dls-5/1
随机推荐
AB PLC学习笔记
7.15 simulation summary
Practice Guide for peanut shell inner net penetration
constrained graphic layout generation via latent optimization
Get started quickly, Jupiter notebook
DzzOffice_ Flowplayer player changes
Common protocols of tcp/ip
Task-Customized Self-Supervised Pre-training with Scalable Dynamic Routing
Dls-42/4-4 dc110v double position relay
singan:learning a generative model from a single natural image
单细胞论文记录(part15)--Integrated analysis of multimodal single-cell data
singan:learning a generative model from a single natural image
Sword finger offer 28 Symmetric binary tree
July 2022 information theory Wu Jun
基于AMDP的BW转换专家例程
Tools to measure the gap between two distributions: cross entropy and KL divergence
DSP-Core类库的.NET版本降级
MySQL triggers and stored procedures
求水仙花数
7.15模擬賽總結