当前位置:网站首页>86. 分隔链表
86. 分隔链表
2022-06-30 05:23:00 【Grayson Zhang】
题目描述
给你一个链表的头节点 head 和一个特定值 x ,请你对链表进行分隔,使得所有 小于 x 的节点都出现在 大于或等于 x 的节点之前。
你应当 保留 两个分区中每个节点的初始相对位置。
示例 1:
输入:head = [1,4,3,2,5,2], x = 3
输出:[1,2,2,4,3,5]
示例 2:
输入:head = [2,1], x = 2
输出:[1,2]
提示:
链表中节点的数目在范围 [0, 200] 内
-100 <= Node.val <= 100
-200 <= x <= 200
题解(C++)
class Solution {
public:
ListNode* partition(ListNode* head, int x) {
ListNode* small = new ListNode(0);
ListNode* smallHead = small;
ListNode* large = new ListNode(0);
ListNode* 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;
}
};

边栏推荐
- [typescript] experimentaldecorators of vscode stepping pit
- Bev instance prediction based on monocular camera (iccv 2021)
- Unity packaging and publishing webgl error reason exception: failed building webgl player
- RedisTemplate 常用方法汇总
- GoLand No Tests Were Run : 不能使用 fmt.Printf() &lt;BUG&gt;
- Virtual and pure virtual destructions
- OpenGL draws model on QT platform to solve the problem of initializing VAO and VBO
- Chapter 9 of OpenGL super classic (version 7): fragment processing and frame buffering
- Records of problems encountered in unity + hololens development
- Nestjs配置静态资源,模板引擎以及Post示例
猜你喜欢

How to judge the quality of network transformer? What symptom is network filter transformer broken?

14x1.5cm竖向标签有点难,VFP调用BarTender来打印

3D rotation album

Unity + hololens2 performance test

企事业单位源代码防泄露工作该如何进行

Unityshader learning notes - Basic Attributes

东塔攻防世界—xss绕过安全狗

Summary of common loss functions in pytorch

Use the code cloud publicholiday project to determine whether a day is a working day

Unity ugui text value suspended enlarged display add text background
随机推荐
Unity mobile end sliding screen rotation
旋转框目标检测mmrotate v0.3.1入门
Unity publishing /build settings
E: Topic focus
Log writing specification
Postman 做测试的 6 个常见问题
Gradient clip in dqn
Unity multiple UI page turning left and right
[notes] unity webgl input Chinese
PWN入门(2)栈溢出基础
Set a plane to camera viewport
OpenGL draws model on QT platform to solve the problem of initializing VAO and VBO
Unity limited time use limited trial time and use times
MinGW-w64下载文件失败the file has been downloaded incorrectly!
Baiwen.com 7 days Internet of things smart home learning experience punch in the third day
《谁动了我的奶酪》读后感
Responsive flow layout
East Tower attack and defense world - XSS bypasses the safety dog
Access is denied encountered when vfpbs calls excel under IIS
Generate a slice of mesh Foundation