当前位置:网站首页>LeetCode Algorithm 86. 分隔鏈錶
LeetCode Algorithm 86. 分隔鏈錶
2022-06-30 10:55:00 【Alex_996】
題目鏈接:86. 分隔鏈錶
Ideas
算法:模擬
數據結構:鏈錶
思路:(我一開始竟然妄圖用一堆指針進行原地交換,成功把自己繞暈然後打開了題解。)
首先還是創建兩個虛擬節點,分別用來維護題目中要求的小於x的節點和大於x的節點,然後遍曆鏈錶,遇到一個節點就判斷是什麼類型,然後把它歸到相應的虛擬節點後面即可。
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;
}
};
边栏推荐
猜你喜欢

Sarsa笔记

Matplotlib notes: contour & Contour

DQN笔记

小程序中读取腾讯文档的表格数据

ArcGIS PRO + PS vectorized land use planning map
![[机缘参悟-34]:光锥之内皆命运](/img/3e/9f5630ba382df7f7ce00705445cef8.jpg)
[机缘参悟-34]:光锥之内皆命运

Dell et Apple, deux entreprises de PC établies, se sont effondrées rapidement

在 sCrypt 中实现高效的椭圆曲线点加法和乘法

历史上的今天:微软收购 PowerPoint 开发商;SGI 和 MIPS 合并

中国将强制统一充电接口,苹果如不低头,iPhone将被踢出中国市场
随机推荐
【Proteus仿真】Arduino UNO LED模拟交通灯
Cp2112 teaching example of using USB to IIC communication
ArcGIS PRO + PS vectorized land use planning map
19:00 p.m. tonight, knowledge empowerment phase 2 live broadcast - control panel interface design of openharmony smart home project
Deep dive kotlin Xie Cheng (17): Actor
Review of mathematical knowledge: curve integral of the second type
LVGL8.2 Simple Checkboxes
再测云原生数据库性能:PolarDB依旧最强,TDSQL-C、GaussDB变化不大
Gd32 RT thread PWM drive function
吴恩达2022机器学习专项课测评来了!
Pycharm项目使用pyinstalle打包过程中问题及解决方案
When does the database need to use the index [Hangzhou multi surveyors] [Hangzhou multi surveyors _ Wang Sir]
SGD有多种改进的形式,为什么大多数论文中仍然用SGD?
腾讯云数据库工程师能力认证重磅推出,各界共话人才培养难题
05_ Node JS file management module FS
[机缘参悟-34]:光锥之内皆命运
Retest the cloud native database performance: polardb is still the strongest, while tdsql-c and gaussdb have little change
js常见问题
Mysql database foundation: TCL transaction control language
LVGL 8.2 Checkboxes as radio buttons