当前位置:网站首页>Leetcode 0143. rearrange linked list
Leetcode 0143. rearrange linked list
2022-07-28 14:16:00 【Tisfy】
【LetMeFly】143. Rearrange the list
Force button topic link :https://leetcode.cn/problems/reorder-list/
Given a single chain table L The head node of head , Single chain list L Expressed as :
L0 → L1 → … → Ln - 1 → Ln
Please rearrange them to :
L0 → Ln → L1 → Ln - 1 → L2 → Ln - 2 → …
You can't just change the value inside the node , It needs to actually exchange nodes .
Example 1:

Input :head = [1,2,3,4] Output :[1,4,2,3]
Example 2:

Input :head = [1,2,3,4,5] Output :[1,5,2,4,3]
Tips :
- The length range of the linked list is
[1, 5 * 104] 1 <= node.val <= 1000
Method 1 : Hashtable
Traversing the linked list , Store the linked list nodes in the hash table , The mapping relationship is <[ The first few nodes , node ]>( In fact, arrays can also be used here , Although the complexity is the same , But the actual cost of arrays is still smaller )
then , With two Pointers l l l and r r r, Point to the nodes of the previous processing and the nodes of the subsequent processing respectively
When the current pointer exceeds the rear pointer , Exit loop .
matters needing attention :
- use head After traversing the linked list ,head It no longer points to the head node , Remember to head place
- Remember to put the last node of the linked list next empty
- Time complexity O ( N 2 ) O(N^2) O(N2)
- Spatial complexity O ( N log N ) O(N\log N) O(NlogN)
AC Code
C++
class Solution {
public:
void reorderList(ListNode* head) {
unordered_map<int, ListNode*> ma;
int cnt = 0;
while (head) {
ma[cnt++] = head;
head = head->next;
}
head = ma[0]; // head place
int l = 1, r = cnt - 1; // To be specified
bool front = false;
while (l <= r) {
if (front) {
head->next = ma[l++];
front = false;
}
else {
head->next = ma[r--];
front = true;
}
head = head->next;
}
head->next = nullptr; // Of the last node next empty
}
};
Synchronous posting on CSDN, Originality is not easy. , Reprint please attach Link to the original text Oh ~
Tisfy:https://letmefly.blog.csdn.net/article/details/126031446
边栏推荐
- SLAM论文合集
- UFIDA BiP CRM new product launch enables large and medium-sized enterprises to grow their marketing
- 记一次COOKIE的伪造登录
- Master closures and consolidate basic skills
- 【Utils】CookieUtil
- [server data recovery] HP StorageWorks series server RAID5 offline data recovery of two disks
- 彻底掌握二分查找
- 开源项目丨Taier1.2版本发布,新增工作流、租户绑定简化等多项功能
- 【Utils】ServletUtil
- Four ways to create thread pools
猜你喜欢

strcmp、strstr、memcpy、memmove的实现

Security assurance is based on software life cycle -istio authorization mechanism

UFIDA BiP CRM new product launch enables large and medium-sized enterprises to grow their marketing

Machine learning (Zhou Zhihua) Chapter 6 notes on Support Vector Learning

Understanding of "image denoising using an improved generic advantageous network with Wasserstein distance"

【服务器数据恢复】HP StorageWorks系列服务器RAID5两块盘离线的数据恢复

83.(cesium之家)cesium示例如何运行

安全保障基于软件全生命周期-PSP应用

Target detection: speed and accuracy comparison (fater r-cnn, r-fcn, SSD, FPN, retinanet and yolov3)

Clickhouse distributed cluster construction
随机推荐
【翻译】盐业公司来Linkerd公司是为了负载平衡,留下来是为了效率、可靠性和性能。...
Qt5 development from introduction to mastery -- the first overview
Power amplifier and matching network learning
7.27 simulation summary
盘点操作URL中常用的几个高效API
关于栈的理解以及实际应用场景
数据库优化 理解这些就够了
正则表达式
Understand BFC features and easily realize adaptive layout
Solve the problem that uniapp wechat applet canvas cannot introduce fonts
Diablo 4 ps4/ps5 beta has been added to the Playstation database
Do you really know esmodule
成为绿色数据中心新样板,东莞华为云数据中心是怎样炼成的?
Thesis study -- masked generative disintegration
Understanding of "image denoising using an improved generic advantageous network with Wasserstein distance"
URL related knowledge points
How did Dongguan Huawei cloud data center become a new model of green data center?
Read how to deploy highly available k3s with external database
安全保障基于软件全生命周期-Istio的认证机制
阿里、京东、抖音:把云推向产业心脏