当前位置:网站首页>86. 分隔链表
86. 分隔链表
2022-06-11 08:55:00 【拽拽就是我】
leetcode力扣刷题打卡
题目:86. 分隔链表
描述:给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。
你应当 保留 两个分区中每个节点的初始相对位置。
解题思路
1、遍历链表,用两个数组分别存储小于x的节点以及大于等于x的节点;
2、链表结尾一定要指向空指针,p->next = nullptr;
原代码##
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
vector<ListNode *> v1, v2;
ListNode *p = new ListNode(-1);
ListNode *ans = p;
while (head) {
if (head->val < x) v1.emplace_back(head);
else v2.emplace_back(head);
head = head->next;
}
for (int i = 0; i < v1.size(); ++i) {
p->next = v1[i];
p = p->next;
}
for (int j = 0; j < v2.size(); ++j) {
p->next = v2[j];
p = p->next;
}
p->next = nullptr;
return ans->next;
}
};
边栏推荐
- M1 chip guide: M1, M1 pro, M1 Max and M1 ultra
- 窗帘做EN 1101易燃性测试过程是怎么样的?
- File system check of the root filesystem failed
- LiveData 与 StateFlow,我该用哪个?
- 复制的代码格式混乱怎么办?
- Display DIN 4102-1 Class B1 fire test requirements
- How to apply for BS 476-7 sample for display? Is it the same as the display
- leetcode - 460. LFU 缓存
- Matlab learning 8- linear and nonlinear sharpening filtering and nonlinear smoothing filtering of image processing
- 木板ISO 5660-1 热量释放速率摸底测试
猜你喜欢

剑指 Offer 51. 数组中的逆序对

leetcode - 460. LFU 缓存

【C语言-函数栈帧】从反汇编的角度,剖析函数调用全流程

Iso8191 test is mentioned in as 3744.1. Are the two tests the same?

Sword finger offer 62 The last remaining number in the circle

leetcode - 739. Daily temperature

What is the process of en 1101 flammability test for curtains?

Matlab学习8-图像处理之线性与非线性锐化滤波、非线性平滑滤波

leetcode - 739. 每日温度

Not eligible for getting processed by all beanpostprocessors
随机推荐
win10家庭版如何连接远程桌面
What software is required to process raw format images?
c语言打印心形
MySQL & Oracle database capacity query
Hibernate L2 cache
Display DIN 4102-1 Class B1 fire test requirements
Sword finger offer 40 Minimum number of K
Codeworks round 723 (Div. 2)
844. 比较含退格的字符串
Interprocess communication
The leader asked me to rewrite the test code. Should I do the same?
PHP解决中文显示乱码
20. 有效的括号
Notes on MySQL core points
File system check of the root filesystem failed
leetcode - 230. 二叉搜索树中第K小的元素
Which Apple devices support this system update? See if your old apple device supports the latest system
显示屏DIN 4102-1 Class B1防火测试要求
Matlab r2022a installation tutorial
SAP abap内表分类与增删改查操作