当前位置:网站首页>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
边栏推荐
- 数据库系统概论(第5版)补充习题——第一章 绪论
- Custom Configuration Sections
- Niuke multi school link with level editor i- (linear DP)
- 【Utils】CookieUtil
- 文献阅读(245)Roller
- 什么是自旋锁 自旋锁是指当一个线程尝试获取某个锁时,如果该锁已被其他线程占用,就一直循环检测锁是否被释放,而不是进入线程挂起或睡眠状态。 /** * 为什么用自旋锁:多个线程对同一个变量
- Foundation of deep learning ---- GNN spectral domain and airspace (continuous improvement, update and accumulation)
- 【Utils】ServletUtil
- 3种方法解轮转数组
- 如何有效进行回顾会议(上)?
猜你喜欢

Clickhouse分布式集群搭建

QT自制软键盘 最完美、最简单、跟自带虚拟键盘一样

成为绿色数据中心新样板,东莞华为云数据中心是怎样炼成的?

RSA encrypts data with private key and decrypts data with public key (not a signature verification process)

strcmp、strstr、memcpy、memmove的实现

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

用友BIP CRM新品发布,赋能大中型企业营销增长

《机器学习》(周志华) 第6章 支持向量 学习心得 笔记

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

Multi level cache scheme
随机推荐
Machine learning (Zhou Zhihua) Chapter 6 notes on Support Vector Learning
LeetCode 1331.数组序号转换
解决uniapp微信小程序canvas不能引入字体的问题
Target detection: speed and accuracy comparison (fater r-cnn, r-fcn, SSD, FPN, retinanet and yolov3)
Verification code brute force cracking test [easy to understand]
IP黑白名单
Using reflection to build a menu spanning tree
UFIDA BiP CRM new product launch enables large and medium-sized enterprises to grow their marketing
A label_ File download (download attribute)
URL related knowledge points
MySQL development skills - View
基于NoneBot2的qq机器人配置记录
【Utils】FastDFS工具类
Postgresql14安装及主从配置
MySql5.5之后的默认存储引擎为InnoDB。
Several efficient APIs commonly used in inventory operation URL
Daily question - Scholarship
彻底掌握二分查找
[basic course of flight control development 7] crazy shell · open source formation UAV SPI (barometer data acquisition)
How to configure ADB environment variables (where to open environment variables)