当前位置:网站首页>LeetCode Algorithm 86. Separate linked list
LeetCode Algorithm 86. Separate linked list
2022-06-30 10:55:00 【Alex_ 12 hours a day 6 days a week】
Topic link :86. Separate the list
Ideas
Algorithm : simulation
data structure : Linked list
Ideas :( At first, I tried to exchange in place with a bunch of pointers , I successfully dizzy myself and then opened the question .)
First, create two virtual nodes , It is respectively used to maintain the requirements in the topic less than x Node sum of is greater than x The node of , Then iterate through the list , When you encounter a node, you can judge what type it is , Then put it behind the corresponding virtual node .
Code
C++
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode *small = new ListNode(0), *large = new ListNode(0);
ListNode *smallHead = small, *largeHead = large;
while (head != nullptr) {
if (head->val < x) {
small->next = head;
small = small->next;
} else {
large->next = head;
large = large->next;
}
head = head->next;
}
large->next = nullptr;
small->next = largeHead->next;
return smallHead->next;
}
};
边栏推荐
- SGD有多种改进的形式,为什么大多数论文中仍然用SGD?
- 智能DNA分子纳米机器人模型来了
- [rust daily] several new libraries were released on January 23, 2021
- 透過華為軍團看科技之變(五):智慧園區
- 电商两位大佬花边新闻刷屏,代表电商回归正常,将有利于实体经济
- Overview of currency
- CSDN daily one practice 2021.11.06 question 1 (C language)
- 第一届中国数字藏品大会即将召开
- Kernel linked list (general linked list) "list.h" simple version and individual comments
- 20万奖金池!【阿里安全 × ICDM 2022】大规模电商图上的风险商品检测赛火热报名中!...
猜你喜欢

苹果高管公然“开怼”:三星抄袭 iPhone,只加了个大屏

go-zero微服务实战系列(八、如何处理每秒上万次的下单请求)

Pytorch Notebook. Nn. Batchnorm1d

在IPhone12的推理延迟仅为1.6 ms!Snap等详析Transformer结构延迟,并用NAS搜出移动设备的高效网络结构...

深潜Kotlin协程(十八):冷热数据流

Android 开发面试真题进阶版(附答案解析)

Unity Shader - 踩坑 - BRP 管线中的 depth texture 的精度问题(暂无解决方案,推荐换 URP)

小程序中读取腾讯文档的表格数据
![When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]](/img/2a/f07a7006e0259d78d046b30c761764.jpg)
When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]

Go zero micro Service Practice Series (VIII. How to handle tens of thousands of order requests per second)
随机推荐
【Proteus仿真】Arduino UNO LED模拟交通灯
Android 开发面试真题进阶版(附答案解析)
Unity Shader - 踩坑 - BRP 管线中的 depth texture 的精度问题(暂无解决方案,推荐换 URP)
Pandora IOT development board learning (HAL Library) - Experiment 1 running lantern (RGB) experiment (learning notes)
Use keil5 software to simulate and debug gd32f305 from 0
The two e-commerce bigwigs' lacy news screens represent the return of e-commerce to normal, which will be beneficial to the real economy
Smith chart view of semi steel coaxial RF line and RF line matching calibration of network analyzer e5071c
透過華為軍團看科技之變(五):智慧園區
[deep learning] common methods for deep learning to detect small targets
59 websites programmers need to know
Gd32 RT thread flash driver function
ionic4 ion-reorder-group组件拖拽改变item顺序
CSDN blog operation team 2022 H1 summary
Double-DQN笔记
LVGL 8.2 Simple Drop down list
DQN笔记
Mysql database foundation: TCL transaction control language
MySQL从入门到精通50讲(三十二)-ScyllaDB生产环境集群搭建
Gd32 RT thread ota/bootloader driver function
matplotlib 笔记: contourf & contour